perm filename FOO.XGP[206,LSP] blob
sn#258434 filedate 1977-01-06 generic text, type T, neo UTF8
/LMAR=0/XLINE=3/FONT#0=BAXL30/FONT#1=BDR30/FONT#2=BAXM30/FONT#3=BASB30/FONT#4=BDR30/FONT#5=SUB/FONT#6=SUP/FONT#9=FIX40/FONT#10=FIX30/FONT#11=GRFX25/FONT#12=GRFX35
␈↓ ↓H␈↓β␈↓ ∧7RECURSIVE PROGRAMMING IN LISP
␈↓ ↓H␈↓β␈↓ ¬Rby John McCarthy
␈↓ ↓H␈↓β␈↓ ¬IStanford University
␈↓ ↓H␈↓β␈↓ ∧hCopyright 1976 by John McCarthy
␈↓ ↓H␈↓␈↓ ∧⊗This version printed at 1:02 on January 6, 1977.
␈↓ ↓H␈↓␈↓ εP␈↓ Oi
␈↓ ↓H␈↓␈↓ ∧P␈↓&T A B L E O F C O N T E N T S␈↓)αβ
␈↓ ↓H␈↓SECTION␈↓ PAGE
␈↓ ↓H␈↓I␈↓ α8INTRODUCTION TO LISP
␈↓ ↓H␈↓␈↓ βλ1␈↓ ∧(Lists.␈↓ ¬_. . . . . . . . . . . . . . . . ␈↓ h␈↓ ? 2
␈↓ ↓H␈↓␈↓ βλ2␈↓ ∧(Atoms.␈↓ ¬_. . . . . . . . . . . . . . . . ␈↓ h␈↓ ? 4
␈↓ ↓H␈↓␈↓ βλ3␈↓ ∧(List structures.␈↓ ¬x. . . . . . . . . . . . . ␈↓ h␈↓ ? 4
␈↓ ↓H␈↓␈↓ βλ4␈↓ ∧(S-expressions.␈↓ ¬x. . . . . . . . . . . . . ␈↓ h␈↓ ? 5
␈↓ ↓H␈↓␈↓ βλ5␈↓ ∧(The basic functions and predicates of LISP.␈↓ λx. . . ␈↓ h␈↓ ? 8
␈↓ ↓H␈↓␈↓ βλ6␈↓ ∧(Conditional expressions.␈↓ εX. . . . . . . . . . . ␈↓ h␈↓ 0 10
␈↓ ↓H␈↓␈↓ βλ7␈↓ ∧(Boolean expressions.␈↓ ε(. . . . . . . . . . . . ␈↓ h␈↓ 0 12
␈↓ ↓H␈↓␈↓ βλ8␈↓ ∧(Recursive function definitions.␈↓ π8. . . . . . . . ␈↓ h␈↓ 0 13
␈↓ ↓H␈↓␈↓ βλ9␈↓ ∧(Lambda expressions and functions with functions as
␈↓ ↓H␈↓␈↓ ¬(arguments.␈↓ ε(. . . . . . . . . . . . ␈↓ h␈↓ 0 19
␈↓ ↓H␈↓␈↓ βλ10␈↓ ∧(Label.␈↓ ¬_. . . . . . . . . . . . . . . . ␈↓ h␈↓ 0 22
␈↓ ↓H␈↓␈↓ βλ11␈↓ ∧(Numerical computation.␈↓ εX. . . . . . . . . . . ␈↓ h␈↓ 0 23
␈↓ ↓H␈↓II␈↓ α8HOW TO WRITE RECURSIVE FUNCTION DEFINITIONS
␈↓ ↓H␈↓␈↓ βλ1␈↓ ∧(Static and dynamic ways of programming.␈↓ λH. . . . ␈↓ h␈↓ 0 25
␈↓ ↓H␈↓␈↓ βλ2␈↓ ∧(Simple list recursion.␈↓ ε(. . . . . . . . . . . . ␈↓ h␈↓ 0 26
␈↓ ↓H␈↓␈↓ βλ3␈↓ ∧(Simple S-expression recursion.␈↓ π8. . . . . . . . ␈↓ h␈↓ 0 28
␈↓ ↓H␈↓␈↓ βλ4␈↓ ∧(Other structural recursions.␈↓ πλ. . . . . . . . . ␈↓ h␈↓ 0 29
␈↓ ↓H␈↓␈↓ βλ5␈↓ ∧(Tree search recursion.␈↓ εX. . . . . . . . . . . ␈↓ h␈↓ 0 30
␈↓ ↓H␈↓␈↓ ¬ TABLE OF CONTENTS␈↓ Fii
␈↓ ↓H␈↓␈↓ βλ6␈↓ ∧(Game trees.␈↓ ¬H. . . . . . . . . . . . . . . ␈↓ h␈↓ 0 34
␈↓ ↓H␈↓III␈↓ α8COMPILING IN LISP
␈↓ ↓H␈↓␈↓ βλ1␈↓ ∧(Introduction␈↓ ¬H. . . . . . . . . . . . . . . ␈↓ h␈↓ 0 38
␈↓ ↓H␈↓␈↓ βλ2␈↓ ∧(Some facts about the PDP-10.␈↓ π8. . . . . . . . ␈↓ h␈↓ 0 39
␈↓ ↓H␈↓␈↓ βλ3␈↓ ∧(Code produced by LISP compilers.␈↓ πh. . . . . . . ␈↓ h␈↓ 0 40
␈↓ ↓H␈↓␈↓ βλ4␈↓ ∧(Listings of LCOM0 and LCOM4␈↓ πh. . . . . . . ␈↓ h␈↓ 0 43
␈↓ ↓H␈↓IV␈↓ α8COMPUTABILITY
␈↓ ↓H␈↓␈↓ βλ1␈↓ ∧(The function ␈↓αeval␈↓.␈↓ ε(. . . . . . . . . . . . ␈↓ h␈↓ 0 67
␈↓ ↓H␈↓␈↓ βλ2␈↓ ∧(Computability.␈↓ ¬x. . . . . . . . . . . . . ␈↓ h␈↓ 0 70
␈↓ ↓H␈↓V␈↓ α8PROVING LISP PROGRAMS CORRECT
␈↓ ↓H␈↓␈↓ βλ1␈↓ ∧(First order logic with conditional forms and lambda-
␈↓ ↓H␈↓␈↓ ¬(expressions.␈↓ εX. . . . . . . . . . . ␈↓ h␈↓ 0 73
␈↓ ↓H␈↓␈↓ βλ2␈↓ ∧(Conditional forms.␈↓ ε(. . . . . . . . . . . . ␈↓ h␈↓ 0 75
␈↓ ↓H␈↓␈↓ βλ3␈↓ ∧(Lambda-expressions.␈↓ εX. . . . . . . . . . . ␈↓ h␈↓ 0 77
␈↓ ↓H␈↓␈↓ βλ4␈↓ ∧(Algebraic axioms for S-expressions and lists.␈↓ λx. . . ␈↓ h␈↓ 0 77
␈↓ ↓H␈↓␈↓ βλ5␈↓ ∧(Axiom schemas of induction.␈↓ π8. . . . . . . . ␈↓ h␈↓ 0 78
␈↓ ↓H␈↓␈↓ βλ6␈↓ ∧(Proofs by structural induction.␈↓ π8. . . . . . . . ␈↓ h␈↓ 0 79
␈↓ ↓H␈↓␈↓ εP␈↓ I2
␈↓ ↓H␈↓β␈↓ ¬yCHAPTER I
␈↓ ↓H␈↓β␈↓ ¬∞INTRODUCTION TO LISP
␈↓ ↓H␈↓1. ␈↓βLists.␈↓
␈↓ ↓H␈↓ Symbolic␈α
information␈α
in␈α
LISP␈α
is␈α expressed␈α
by␈α
S-expressions␈α
and␈α
these␈α
are␈α represented
␈↓ ↓H␈↓in␈α∂ the␈α∂ memory␈α∞of␈α∂the␈α∂computer␈α∂by␈α∞list␈α∂structures.␈α∂Before␈α∞giving␈α∂formal␈α∂ definitions,␈α∂ we␈α∞ shall
␈↓ ↓H␈↓give some examples.
␈↓ ↓H␈↓ The most common form of S-expression is the list, and here are some lists:
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓ ␈↓∧(A B C E)
␈↓ ↓H␈↓∧␈↓has four elements.
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓ ␈↓∧(A B (C D) E)
␈↓ ↓H␈↓∧␈↓has four elements one of which is itself a list.
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓ ␈↓∧(A)
␈↓ ↓H␈↓∧␈↓has one element.
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓ ␈↓∧((A B C D))
␈↓ ↓H␈↓∧␈↓also has one element which itself is a list.
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓ ␈↓∧()
␈↓ ↓H␈↓∧␈↓has no elements; it is also written
␈↓ ↓H␈↓ ␈↓∧NIL.
␈↓ ↓H␈↓∧␈↓The list
␈↓ ↓H␈↓ ␈↓∧(PLUS X Y)
␈↓ ↓H␈↓∧␈↓has three elements and may be used to represent the expression
␈↓ ↓H␈↓ ␈↓αx␈↓↓ + ␈↓αy.
␈↓ ↓H␈↓α␈↓The list
␈↓ ↓H␈↓ ␈↓∧(PLUS (TIMES X Y) X 3)
␈↓ ↓H␈↓∧␈↓has four elements and may be used to represent the expression
␈↓ ↓H␈↓ ␈↓αxy␈↓↓ + ␈↓αx␈↓↓ + ␈↓∧3.
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ I3
␈↓ ↓H␈↓∧␈↓The list
␈↓ ↓H␈↓ ␈↓∧(EXIST X (ALL Y (IMPLIES (P X) (P Y))))
␈↓ ↓H␈↓∧␈↓may be used to represent the logical expression
␈↓ ↓H␈↓ ␈↓↓(∃␈↓αx␈↓↓)(∀␈↓αy␈↓↓).␈↓αP␈↓↓(␈↓αx␈↓↓)⊃␈↓αP␈↓↓(␈↓αy␈↓↓).
␈↓ ↓H␈↓↓␈↓The list
␈↓ ↓H␈↓ ␈↓∧(INTEGRAL 0 ∞ (TIMES (EXP (TIMES I X Y)) (F X)) X)
␈↓ ↓H␈↓∧␈↓may be used to represent the expression
␈↓ ↓H␈↓ ␈↓ ␈␈↓¬0␈↓ε␈↓#
∞␈↓#␈↓αe␈↓εixy␈↓αf␈↓↓(␈↓αx␈↓↓)␈↓αdx.
␈↓ ↓H␈↓α␈↓The list
␈↓ ↓H␈↓ ␈↓∧((A B) (B A C D) (C B D E) (D B C E) (E C D F) (F E))
␈↓ ↓H␈↓is␈α
used␈αto␈α
represent␈α
the␈αnetwork␈α
of␈α
Figure␈α1␈α
according␈α
to␈α a␈α
scheme␈α
whereby␈α there␈α
is␈α
a␈αsublist
␈↓ ↓H␈↓for each vertex consisting of the vertex itself followed by the vertices to which it is connected.
␈↓"␈↓ ↓H␈↓␈↓ βX C
␈↓"∧␈↓ ↓H␈↓␈↓ βX ≤'~`≥
␈↓"␈↓ ↓H␈↓␈↓ βX ≤' ~ `≥
␈↓"␈↓ ↓H␈↓␈↓ βX B ≤' ~ `≥ E
␈↓"␈↓ ↓H␈↓␈↓ βX A ααααααααα' ~ `ααααααααα F␈↓ ↓H ≥ ≤
␈↓"␈↓ ↓H␈↓␈↓ βX `≥ ~ ≤'
␈↓"␈↓ ↓H␈↓␈↓ βX `≥ ~ ≤'
␈↓"␈↓ ↓H␈↓␈↓ βX `≥~≤'
␈↓"
␈↓ ↓H␈↓␈↓ βX D
␈↓"␈↓ ↓H␈↓␈↓ βX Figure 1
␈↓ ↓H␈↓ The␈α∞ elements␈α∞ of␈α∂ a␈α∞ list␈α∞ are␈α∂surrounded␈α∞by␈α∞parentheses␈α∞and␈α∂separated␈α∞by␈α∞spaces.␈α∂ A␈α∞list
␈↓ ↓H␈↓may␈αhave␈αany␈αnumber␈αof␈αterms␈αand␈αany␈α of␈αthese␈α terms␈α may␈α themselves␈α be␈α lists.␈α In␈α this␈αcase,
␈↓ ↓H␈↓the␈αspaces␈αsurrounding␈αa␈α
sublist␈α may␈α be␈α omitted,␈α and␈α
extra␈α spaces␈α between␈αelements␈αof␈α
a␈αlist
␈↓ ↓H␈↓are allowed. Thus the lists
␈↓ ↓H␈↓ ␈↓∧(A B(C D) E)
␈↓ ↓H␈↓∧␈↓and
␈↓ ↓H␈↓ ␈↓∧(A B (C D) E)
␈↓ ↓H␈↓∧␈↓are regarded as the same.
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ I4
␈↓ ↓H␈↓2. ␈↓βAtoms.␈↓
␈↓ ↓H␈↓ The␈αexpressions␈α␈↓∧A,␈αB,␈αX,␈αY,␈α3,␈αPLUS,␈α␈↓and␈α␈↓∧ALL␈↓␈αoccurring␈αin␈αthe␈αabove␈α lists␈αare␈αcalled␈αatoms.
␈↓ ↓H␈↓In␈α
general,␈α∞an␈α
atom␈α
is␈α∞expressed␈α
by␈α
a␈α∞sequence␈α
of␈α
capital␈α∞letters,␈α
digits,␈α
and␈α∞ special␈α
characters
␈↓ ↓H␈↓with␈α∩certain␈α∩ exclusions.␈α∩ The␈α∩exclusions␈α∩are␈α∩<space>,␈α∩<carriage␈α∩return>,␈α∩and␈α∩the␈α∩other␈α∩non-
␈↓ ↓H␈↓printing␈α∂ characters,␈α∞ and␈α∂ also␈α∞ the␈α∂ parentheses,␈α∞brackets,␈α∂ semi-colon,␈α∞ and␈α∂ comma.␈α∞ Numbers
␈↓ ↓H␈↓are␈α⊂expressed␈α⊂as␈α⊂signed␈α⊂decimal␈α⊂or␈α⊂octal␈α⊂numbers,␈α⊂ the␈α⊂ exact␈α⊂ convention␈α⊂ depending␈α⊃ on␈α⊂ the
␈↓ ↓H␈↓implementation.␈α~ Floating␈α~ point␈α~ numbers␈α~ are␈α~ written␈α~ with␈α~decimal␈α~points␈α~and,␈α~when
␈↓ ↓H␈↓appropriate,␈α∪an␈α∪exponent␈α∪notation␈α∪depending␈α∪ on␈α∪ the␈α∪implementation.␈α∪ The␈α∪ reader␈α∩ should
␈↓ ↓H␈↓consult the programmer's manual for the LISP implementation he intends to use.
␈↓ ↓H␈↓ Some examples of atoms are
␈↓ ↓H␈↓ ␈↓∧THE-LAST-TRUMP
␈↓ ↓H␈↓∧␈↓ ␈↓∧A307B
␈↓ ↓H␈↓∧␈↓ ␈↓∧345
␈↓ ↓H␈↓∧␈↓ ␈↓∧3.14159,
␈↓ ↓H␈↓∧␈↓and from these we can form lists like
␈↓ ↓H␈↓ ((345 3.14159 -47) A307B THE-LAST-TRUMP -45.21).
␈↓ ↓H␈↓3. ␈↓βList structures.␈↓
␈↓ ↓H␈↓ Lists␈α
are␈α
represented␈α
in␈α
the␈α
memory␈α
of␈α
the␈α
computer␈α
by␈α
list␈α
structures.␈α
A␈α
list␈α
structure␈αis␈α
a
␈↓ ↓H␈↓collection␈αof␈αmemory␈αwords␈α each␈αof␈α which␈α is␈α divided␈α into␈α two␈α parts,␈αand␈αeach␈αpart␈αis␈αcapable
␈↓ ↓H␈↓of␈αcontaining␈αan␈αaddress␈αin␈αmemory.␈αThe␈αtwo␈αparts␈αare␈αcalled␈αare␈α called␈αthe␈α a-part␈α and␈α the␈α d-
␈↓ ↓H␈↓part.␈α⊂ There␈α⊂ is␈α⊂ one␈α⊂computer␈α⊃word␈α⊂for␈α⊂each␈α⊂element␈α⊂of␈α⊂the␈α⊃list,␈α⊂and␈α⊂the␈α⊂a-part␈α⊂of␈α⊃the␈α⊂word
␈↓ ↓H␈↓contains␈α⊂the␈α∂ address␈α⊂of␈α∂the␈α⊂list␈α∂or␈α⊂atom␈α∂representing␈α⊂the␈α∂element,␈α⊂and␈α∂the␈α⊂d-part␈α⊂contains␈α∂the
␈↓ ↓H␈↓address␈α
of␈α
the␈α
word␈α
representing␈α
the␈α
next␈α
element␈α
of␈α
the␈α
list.␈α
If␈α
the␈α
list␈α
element␈α
is␈α
itself␈α
a␈α
list,
␈↓ ↓H␈↓then,␈α∞of␈α∞course,␈α∞the␈α∞address␈α∞of␈α
the␈α∞first␈α∞word␈α∞of␈α∞its␈α∞list␈α
structure␈α∞is␈α∞given␈α∞in␈α∞the␈α∞ a-part␈α∞ of␈α
the
␈↓ ↓H␈↓word␈α∂ representing␈α∂ that␈α∂ element.␈α∂ A␈α∂diagram␈α⊂shows␈α∂this␈α∂more␈α∂clearly␈α∂than␈α∂words,␈α∂and␈α⊂the␈α∂list
␈↓ ↓H␈↓structure␈α∩corresponding␈α∩to␈α∩the␈α∩ list␈α∩ ␈↓∧(PLUS␈α∩(TIMES␈α⊃ X␈α∩ Y)␈α∩ X␈α∩ 3)␈↓␈α∩ which␈α∩may␈α∩represent␈α⊃the
␈↓ ↓H␈↓expression ␈↓αxy␈↓↓ + ␈↓αx␈↓↓ + ␈↓∧3 is shown in figure 2.
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ I5
␈↓"␈↓ ↓H␈↓ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ααααα→~ ~ εαααα→~ ~ εαααα→~ ~ εαααα→~ ~ εααααααα⊃
␈↓"␈↓ ↓H␈↓ %απα∀ααα$ %απα∀ααα$ %απα∀ααα$ %απα∀ααα$ ~
␈↓"␈↓ ↓H␈↓ ↓ ~ ~ ↓ ~
␈↓"␈↓ ↓H␈↓ PLUS ~ ~ 3 ~
␈↓"␈↓ ↓H␈↓ ~ ⊂αααπααα⊃ ~ ⊂αααπααα⊃ ⊂αααπααα⊃ ~
␈↓"␈↓ ↓H␈↓ %α→~ ~ εααβα→~ ~ εαααα→~ ~ ~ ~
␈↓"␈↓ ↓H␈↓ %απα∀ααα$ ~ %απα∀ααα$ %απα∀απα$ ~
␈↓"␈↓ ↓H␈↓ ↓ ~ ~ ↓ ~ ~
␈↓"␈↓ ↓H␈↓ TIMES %απαα$ Y %ααπα$
␈↓"␈↓ ↓H␈↓ ↓ ↓
␈↓"␈↓ ↓H␈↓ X NIL
␈↓"␈↓ ↓H␈↓ Figure 2.
␈↓ ↓H␈↓∧␈↓ ␈↓∧Atoms␈α⊗ are␈α↔ represented␈α⊗ by␈α↔ the␈α⊗ addresses␈α⊗of␈α↔their␈α⊗property␈α↔lists␈α⊗which␈α↔are␈α⊗list
␈↓ ↓H␈↓∧structures␈αof␈αa␈αspecial␈αkind␈α depending␈α on␈α the␈αimplementation.␈α (In␈α some␈α implementations,␈α the
␈↓ ↓H␈↓∧first␈α∪ word␈α∪ of␈α∪a␈α∪property␈α∪list␈α∪is␈α∪in␈α∪a␈α∪special␈α∪are␈α∪of␈α∪memory,␈α∪in␈α∪others␈α∪the␈α∪first␈α∀word␈α∪is
␈↓ ↓H␈↓∧distinguished␈α∂ by␈α∞ sign,␈α∂in␈α∞still␈α∂others␈α∞it␈α∂has␈α∂a␈α∞special␈α∂a-part.␈α∞ For␈α∂basic␈α∞LISP␈α∂programming,␈α∂it␈α∞is
␈↓ ↓H␈↓∧enough␈α
to␈α
know␈α
that␈α∞ atoms␈α
are␈α
distinguishable␈α
from␈α∞ other␈α
list␈α
structures␈α
by␈α∞a␈α
predicate
␈↓ ↓H␈↓∧called ␈↓βat␈↓.)
␈↓ ↓H␈↓ The␈α last␈α
word␈α of␈α
a␈αlist␈α
cannot␈αhave␈α
the␈αaddress␈αof␈α
a␈αnext␈α
word␈αin␈α
its␈αd-part␈α
since␈αthere
␈↓ ↓H␈↓isn't any next word, so it has the address of a special atom called ␈↓∧NIL␈↓.
␈↓ ↓H␈↓ A␈α∪ list␈α∪ is␈α∪ referred␈α∪ to␈α∪ by␈α∪giving␈α∪the␈α∪address␈α∪of␈α∪its␈α∪first␈α∪element.␈α∪ According␈α∪to␈α∪this
␈↓ ↓H␈↓convention,␈α∂we␈α∂see␈α∂that␈α∂the␈α∂a-part␈α∂ of␈α∂ a␈α∂list␈α∂ word␈α∂ is␈α∂ the␈α∂ list␈α∂ element␈α∂ and␈α∂ the␈α∂d-part␈α∂is␈α∂a
␈↓ ↓H␈↓pointer␈αto␈αa␈αsublist␈αformed␈αby␈αdeleting␈αthe␈αfirst␈αelement.␈α Thus␈αthe␈αfirst␈αword␈αof␈αthe␈α list␈α structure
␈↓ ↓H␈↓of␈α figure␈α 2␈α contains␈α a␈α pointer␈αto␈αthe␈αlist␈αstructure␈αrepresenting␈αthe␈αatom␈α ␈↓∧PLUS␈↓,␈αwhile␈αits␈α
d-part
␈↓ ↓H␈↓points␈α∂to␈α∂the␈α∞list␈α∂ ␈↓∧((TIMES␈α∂X␈α∞Y)␈α∂X␈α∂3)␈↓.␈α∞ The␈α∂second␈α∂word␈α∞contains␈α∂the␈α∂list␈α∂structure␈α∞representing
␈↓ ↓H␈↓␈↓∧(TIMES␈α
X␈α
Y)␈↓␈α
in␈α
its␈α
a-part␈α
and␈α
the␈α
list␈α
structure␈α
representing␈α
␈↓∧(X␈α
3)␈↓␈α
in␈α
its␈α
d-part.␈α∞ The␈α
last
␈↓ ↓H␈↓word␈αpoints␈αto␈αthe␈α
atom␈α␈↓∧3␈↓␈α in␈αits␈α
a-part␈αand␈αhas␈αa␈α
pointer␈αto␈αthe␈αatom␈α
␈↓∧NIL␈↓␈α in␈αits␈α d-part.␈α This␈α
is
␈↓ ↓H␈↓consistent with the convention that ␈↓∧NIL␈↓ represents the null list.
␈↓ ↓H␈↓4. ␈↓βS-expressions.␈↓
␈↓ ↓H␈↓ When␈α∂we␈α∂examine␈α⊂the␈α∂way␈α∂list␈α⊂structures␈α∂ represent␈α∂ lists␈α⊂ we␈α∂see␈α∂ a␈α⊂ curious␈α∂ asymmetry.
␈↓ ↓H␈↓Namely,␈α the␈α a-part␈αof␈α
a␈αlist␈αword␈αcan␈α
contain␈αan␈αatom␈αor␈α
a␈αlist,␈αbut␈αthe␈α
d-part␈αcan␈αcontain␈αonly␈α
a
␈↓ ↓H␈↓list␈α∂ or␈α∂the␈α∞special␈α∂atom␈α∂ ␈↓∧NIL␈↓.␈α∂ This␈α∞restriction␈α∂is␈α∂quite␈α∂unnatural␈α∞from␈α∂the␈α∂computing␈α∂point␈α∞of
␈↓ ↓H␈↓view,␈α
and␈α
we␈α shall␈α
allow␈α
arbitrary␈α atoms␈α
to␈α
inhabit␈α the␈α
d-parts␈α
of␈α words,␈α
but␈α
then␈αwe␈α
must
␈↓ ↓H␈↓generalize␈αthe␈αway␈αlist␈αstructures␈αare␈αexpressed␈αas␈αcharacter␈αstrings.␈αTo␈α do␈α this,␈α we␈αintroduce␈αthe
␈↓ ↓H␈↓notion of S-expression.
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ I6
␈↓ ↓H␈↓ An␈α∪ S-expression␈α∪is␈α∩either␈α∪an␈α∪atom␈α∪or␈α∩a␈α∪pair␈α∪of␈α∩S-expressions␈α∪separated␈α∪by␈α∪" . "␈α∩and
␈↓ ↓H␈↓surrounded by parentheses. In BNF, we can write
␈↓ ↓H␈↓<S-expression> ::= <atom> | (<S-expression> . <S-expression>).
␈↓ ↓H␈↓Examples of S-expressions are
␈↓ ↓H␈↓ ␈↓∧A
␈↓ ↓H␈↓∧␈↓ ␈↓∧(A . B)
␈↓ ↓H␈↓∧␈↓ ␈↓∧(A . (B . A))
␈↓ ↓H␈↓∧␈↓ ␈↓∧(PLUS (X . (Y . NIL)))
␈↓ ↓H␈↓∧␈↓ ␈↓∧(3 . 3.4)
␈↓ ↓H␈↓∧␈↓The␈α⊃spaces␈α⊃around␈α⊃the␈α⊃.␈α⊃may␈α⊃be␈α⊃ omitted␈α⊃ when␈α⊃ this␈α⊃ will␈α⊃ not␈α⊃ cause␈α⊃confusion.␈α∩ The␈α⊃only
␈↓ ↓H␈↓possible␈α∞confusion␈α
is␈α∞of␈α
the␈α∞dot␈α
separator␈α∞with␈α∞a␈α
decimal␈α∞point␈α
in␈α∞numbers.␈α
Thus,␈α∞in␈α∞the␈α
above
␈↓ ↓H␈↓cases,␈α
we␈α
may␈α∞ write␈α
␈↓∧(A.B),␈α
(A.(B.A))␈↓,␈α∞ and␈α
␈↓∧(PLUS.(X.(Y.NIL)))␈↓,␈α
but␈α∞if␈α
we␈α
wrote␈α∞␈↓∧(3.3.4)␈↓␈α
confusion
␈↓ ↓H␈↓would result.
␈↓ ↓H␈↓ In␈α
the␈α∞memory␈α
of␈α∞a␈α
computer,␈α∞an␈α
S-expression␈α∞ is␈α
represented␈α∞by␈α
the␈α∞ address␈α
of␈α∞a␈α
word
␈↓ ↓H␈↓whose␈αa-part␈α
contains␈αthe␈αfirst␈α
element␈αof␈αthe␈α
pair␈αand␈αwhose␈α
d-part␈αcontains␈αthe␈α
second␈αelement
␈↓ ↓H␈↓of␈α the␈α
pair.␈α Thus,␈α
the␈αS-expressions␈α ␈↓∧(A.B),␈α
(A.(B.A))␈↓,␈αand␈α
␈↓∧(PLUS.(X.(Y.NIL)))␈α␈↓␈α
are␈αrepresented
␈↓ ↓H␈↓by the list structures of figure 3.
␈↓"␈↓ ↓H␈↓ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ~ ~ ~ ~ ~ εαααα→~ ~ ~
␈↓"␈↓ ↓H␈↓ %απα∀απα$ %απα∀ααα$ %απα∀απα$
␈↓"␈↓ ↓H␈↓ ↓ ↓ ~ ↓ ~
␈↓"␈↓ ↓H␈↓ A B ~ B ~
␈↓"␈↓ ↓H␈↓ ~ ~
␈↓"␈↓ ↓H␈↓ ~ ~
␈↓"␈↓ ↓H␈↓ %ααααααααπαααααααα$
␈↓"␈↓ ↓H␈↓ ↓
␈↓"␈↓ ↓H␈↓ A
␈↓"␈↓ ↓H␈↓ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ~ ~ εαααα→~ ~ εαααα→~ ~ ~
␈↓"␈↓ ↓H␈↓ %απα∀ααα$ %απα∀ααα$ %απα∀απα$
␈↓"␈↓ ↓H␈↓ ↓ ↓ ↓ ↓
␈↓"␈↓ ↓H␈↓ PLUS X Y NIL
␈↓"␈↓ ↓H␈↓ Figure 3.
␈↓ ↓H␈↓ Note␈αthat␈αthe␈αlist␈α␈↓∧(PLUS␈αX␈αY)␈↓␈αand␈αthe␈αS-expression␈α␈↓∧␈α(PLUS␈α.␈α(X␈α.␈α(Y␈α.␈αNIL)))␈↓␈α are␈αrepresented
␈↓ ↓H␈↓in␈α memory␈α by␈α the␈α same␈α list␈αstructure.␈α The␈αsimplest␈αway␈αto␈αtreat␈αthis␈αis␈αto␈αregard␈αS-expressions
␈↓ ↓H␈↓as␈α
primary␈α
and␈α
lists␈α
as␈α
abbreviations␈α for␈α
certain␈α
S-expressions,␈α
namely␈α
those␈α
that␈α
never␈αhave
␈↓ ↓H␈↓any␈αatom␈αbut␈α NIL␈α
as␈αthe␈αsecond␈αpart␈αof␈α
a␈αpair.␈αIn␈αgiving␈α input␈α
to␈α LISP,␈α either␈α the␈α list␈α
form
␈↓ ↓H␈↓or␈α the␈α
S-expression␈α form␈αmay␈α
be␈αused␈αfor␈α
lists.␈α On␈αoutput,␈α
LISP␈αwill␈αprint␈α
a␈αlist␈αstructure␈α
as␈αa
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ I7
␈↓ ↓H␈↓list␈α as␈α far␈α as␈α it␈α can,␈α otherwise␈α as␈α an␈αS-expression.␈α Thus,␈α some␈αlist␈αstructures␈αwill␈αbe␈αprinted
␈↓ ↓H␈↓in a mixed notation, e.g. ␈↓∧((A . B) (C . D) (3))␈↓.
␈↓ ↓H␈↓ In␈αgeneral,␈αthe␈αlist␈α␈↓↓(␈↓αa␈αb␈α.␈α.␈α.␈α z␈↓↓)␈↓␈αmay␈αbe␈αregarded␈αas␈αan␈αabbreviation␈αof␈αthe␈αS-expression␈α␈↓↓(␈↓αa␈↓↓␈α.
␈↓ ↓H␈↓↓(␈↓αb␈↓↓ . (␈↓αc␈↓↓ . (... (␈↓αz . ␈↓∧NIL␈↓↓) ...)))␈↓.
␈↓ ↓H␈↓ Exercises
␈↓ ↓H␈↓ 1. If we represent sums and products as indicated above and
␈↓ ↓H␈↓use␈α
␈↓∧(MINUS␈α
X),␈α
(QUOTIENT␈α
X␈αY)␈↓,␈α
and␈α
␈↓∧(POWER␈α
X␈α
Y)␈↓␈α as␈α
representations␈α
of␈α
␈↓↓-␈↓αx␈↓,␈α
␈↓αx␈↓↓/␈↓αy␈↓,␈α
and␈α ␈↓αx␈↓εy␈↓
␈↓ ↓H␈↓respectively, then
␈↓ ↓H␈↓ a. What do the lists
␈↓ ↓H␈↓ ␈↓∧(QUOTIENT 2 (POWER (PLUS X (MINUS Y)) 3))
␈↓ ↓H␈↓∧␈↓and
␈↓ ↓H␈↓ ␈↓∧(PLUS -2 (MINUS 2) (TIMES X (POWER Y 3.3)))
␈↓ ↓H␈↓∧␈↓represent?
␈↓ ↓H␈↓ b. How are the expressions ␈↓αxyz␈↓↓+␈↓∧3␈↓↓(␈↓αu␈↓↓+␈↓αv␈↓↓)␈↓ε-3␈↓ and ␈↓↓(␈↓αxy␈↓↓-␈↓αyx␈↓↓)/(␈↓αxy␈↓↓+␈↓αyx␈↓↓)␈↓ to be represented?
␈↓ ↓H␈↓ 2. In the above mentioned graph notation, what graph is represented by the list
␈↓ ↓H␈↓ ␈↓∧((A D E F)(B D E F)(C D E F)(D A B C)(E A B C)(F A B C))?
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓3.␈αWrite␈αthe␈αlist␈α␈↓∧((PLUS␈α(TIMES␈αX␈αY)␈αX␈α3)␈↓␈αas␈αan␈αS-expression.␈α This␈αis␈αsometimes␈αreferred␈αto
␈↓ ↓H␈↓as "dot-notation".
␈↓ ↓H␈↓ 4. Write the following S-expressions in list notation to whatever extent is possible:
␈↓ ↓H␈↓ a. (A . NIL)
␈↓ ↓H␈↓ b. (A . B)
␈↓ ↓H␈↓ c. ((A . NIL) . B)
␈↓ ↓H␈↓ d. ((A . B) . ((C . D) . NIL).
␈↓ ↓H␈↓ 5.␈αProve␈αthat␈αthe␈αnumber␈αof␈α"shapes"␈αof␈αS-expressions␈αwith␈α␈↓αn␈↓␈αatoms␈αis␈α␈↓α(2n␈α-␈α2)!/(n!(n␈α-␈α1)!)␈↓.
␈↓ ↓H␈↓(The two shapes of S-expressions with three atoms are ␈↓∧(A.(B.C))␈↓ and ␈↓∧((A.B).C)␈↓).
␈↓ ↓H␈↓ 6.␈αThe␈α
above␈αresult␈αcan␈α
also␈αbe␈αobtained␈α
by␈αwriting␈α␈↓αS = A + S␈↓βx␈↓αS␈↓␈α
as␈αan␈α
"equation"␈αsatisfied
␈↓ ↓H␈↓by␈αthe␈αset␈αof␈α
S-expressions␈αwith␈αthe␈αinterpretation␈α
that␈αan␈αS-expression␈αis␈α
either␈αan␈αatom␈αor␈αa␈α
pair
␈↓ ↓H␈↓of␈αS-expressions.␈α The␈αnext␈αstep␈αis␈αto␈αregard␈αthe␈αequation␈αas␈αthe␈αquadratic␈α␈↓αS␈↓ε2␈↓α - S + A = 0␈↓,␈αsolve␈αit
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ I8
␈↓ ↓H␈↓by␈α∩the␈α∩quadratic␈α∩formula␈α∩choosing␈α∩the␈α∩minus␈α∩sign␈α∩for␈α∩the␈α∩square␈α∩root.␈α∩ Then␈α∩the␈α∩radical␈α⊃is
␈↓ ↓H␈↓regarded␈α∞as␈α∂the␈α∞1/2␈α∞power␈α∂and␈α∞expanded␈α∂by␈α∞the␈α∞binomial␈α∂theorem.␈α∞ Manipulating␈α∂the␈α∞binomial
␈↓ ↓H␈↓coefficients␈α∂yields␈α⊂the␈α∂above␈α∂formula␈α⊂as␈α∂the␈α∂coefficient␈α⊂of␈α∂␈↓αA␈↓εn␈↓␈α∂in␈α⊂the␈α∂expansion.␈α∂ Why␈α⊂does␈α∂this
␈↓ ↓H␈↓somewhat ill-motivated and irregular procedure give the right answer?
␈↓ ↓H␈↓ The last two exercises are unconnected with subsequent material in these notes.
␈↓ ↓H␈↓5. ␈↓βThe basic functions and predicates of LISP.␈↓
␈↓ ↓H␈↓ The␈α∂main␈α∂form␈α∞of␈α∂LISP␈α∂program␈α∂is␈α∞the␈α∂LISP␈α∂function,␈α∂and␈α∞LISP␈α∂functions␈α∂are␈α∂built␈α∞up
␈↓ ↓H␈↓from␈αbasic␈αLISP␈αfunctions,␈αpredicates,␈αvariables␈αand␈αconstants.␈α An␈αexpression␈αwith␈αvariables␈αin␈αit
␈↓ ↓H␈↓whose␈α∀value␈α∀depends␈α∀on␈α∀the␈α∀values␈α∀of␈α∀the␈α∀variables␈α∀is␈α∀called␈α∀a␈α∀␈↓αform␈↓.␈α∀ LISP␈α∀functions␈α∪are
␈↓ ↓H␈↓constructed␈α∩from␈α⊃LISP␈α∩␈↓αforms␈↓␈α⊃which␈α∩are␈α⊃written␈α∩in␈α⊃two␈α∩languages␈α⊃-␈α∩␈↓αpublication␈α∩language␈↓␈α⊃and
␈↓ ↓H␈↓␈↓αinternal␈α∂language␈↓.␈α∂ Publication␈α∂language␈α∂is␈α∂easier␈α⊂for␈α∂people␈α∂to␈α∂read␈α∂and␈α∂write,␈α⊂but␈α∂publication
␈↓ ↓H␈↓language␈α
programs␈α
are␈α
not␈α∞S-expressions,␈α
and␈α
therefore␈α
it␈α
is␈α∞not␈α
easy␈α
to␈α
write␈α
LISP␈α∞programs␈α
to
␈↓ ↓H␈↓generate,␈α∪interpret␈α∪or␈α∪compile␈α∪other␈α∪LISP␈α∩programs␈α∪when␈α∪these␈α∪programs␈α∪are␈α∪in␈α∩publication
␈↓ ↓H␈↓language.␈α The␈αinternal␈αlanguage␈αprograms␈αare␈αS-expressions␈αand␈αare␈αharder␈αfor␈αa␈αperson␈αto␈αread
␈↓ ↓H␈↓and␈αwrite,␈αbut␈αit␈αis␈αeasier␈αfor␈αa␈αperson␈αto␈αwrite␈αa␈αprogram␈αto␈αmanipulate␈α␈↓αobject␈αprograms␈↓␈αwhen␈αthe
␈↓ ↓H␈↓object␈αprograms␈αare␈αin␈αinternal␈αlanguage.␈α Besides␈αall␈αthat,␈αmost␈αLISP␈αimplementations␈αaccept␈αonly
␈↓ ↓H␈↓internal language programs rather than some form of publication language.
␈↓ ↓H␈↓ Just␈α∂as␈α∂numerical␈α∂computer␈α∂programs␈α⊂are␈α∂ based␈α∂ on␈α∂ the␈α∂ four␈α∂arithmetic␈α⊂ operations␈α∂ of
␈↓ ↓H␈↓addition␈α⊂subtraction,␈α⊃multiplication,␈α⊂and␈α⊃division,␈α⊂and␈α⊃the␈α⊂arthmetic␈α⊃predicates␈α⊂of␈α⊃equality␈α⊂and
␈↓ ↓H␈↓comparison,␈α⊂so␈α⊂symbolic␈α⊂ computation␈α⊂ has␈α⊂ its␈α⊂basic␈α⊂predicates␈α⊂and␈α⊂functions.␈α⊂ LISP␈α⊃has␈α⊂three
␈↓ ↓H␈↓basic␈αfunctions␈αand␈αtwo␈α
predicates.␈α Each␈αwill␈αbe␈αexplained␈α
first␈αin␈αits␈αpublication␈αform,␈α
and␈αthen
␈↓ ↓H␈↓the internal form will be given.
␈↓ ↓H␈↓ First,␈α we␈αhave␈αtwo␈αfunctions␈α␈↓βa␈↓␈αand␈α␈↓βd␈↓.␈α ␈↓βa␈α␈↓αx␈↓␈αis␈αthe␈αa-part␈αof␈αthe␈αS-expression␈α␈↓∧x␈↓.␈α Thus,␈α ␈↓βa␈↓∧(A␈α.
␈↓ ↓H␈↓∧B)␈α
␈↓↓=␈α␈↓∧A␈↓,␈α
and␈α␈↓βa␈↓∧((A␈α
.␈α
B)␈α.␈α
A)␈α␈↓↓=␈α
␈↓∧(A␈α
.␈αB)␈↓.␈α
␈↓βd␈α␈↓αx␈↓␈α
is␈α
the␈αd-part␈α
of␈αthe␈α
S-expression␈α
␈↓αx␈↓.␈αThus␈α
␈↓βd␈↓∧(A␈α.␈α
B)␈α
␈↓↓=␈α␈↓∧B␈↓,
␈↓ ↓H␈↓and␈α␈↓βd␈↓∧((A␈α.␈αB)␈α.␈αA)␈α␈↓↓=␈α␈↓∧A␈↓.␈α
␈↓βa␈α␈↓αx␈↓␈αand␈α␈↓βd␈α␈↓αx␈↓␈αare␈αundefined␈αin␈α
basic␈αLISP␈αwhen␈α␈↓αx␈↓␈αis␈αan␈αatom,␈αbut␈αthey␈α
may
␈↓ ↓H␈↓have␈α
a␈α∞meaning␈α
in␈α
some␈α∞implementations.␈α
The␈α∞internal␈α
forms␈α
of␈α∞␈↓βa␈α
␈↓αx␈↓␈α∞and␈α
␈↓βd␈α
␈↓αx␈↓␈α∞are␈α
␈↓∧(CAR␈α∞X)␈↓␈α
and
␈↓ ↓H␈↓␈↓∧(CDR␈α
X)␈↓␈α
respectively.␈α The␈α
names␈α
␈↓∧CAR␈↓␈α
and␈α␈↓∧CDR␈↓␈α
stood␈α
for␈α
"contents␈αof␈α
the␈α
address␈α
part␈αof␈α
register"
␈↓ ↓H␈↓and␈α
"contents␈αof␈α
the␈αdecrement␈α
part␈αof␈α
register"␈αin␈α
a␈α1957␈α
precursor␈αof␈α
LISP␈αprojected␈α
for␈αthe␈α
IBM
␈↓ ↓H␈↓704 computer. The names have persisted for lack of a clearly preferable alternative.
␈↓ ↓H␈↓ A␈α⊃certain␈α∩ambiguity␈α⊃arises␈α∩when␈α⊃we␈α∩want␈α⊃to␈α∩use␈α⊃S-expression␈α∩constants␈α⊃in␈α∩the␈α⊃internal
␈↓ ↓H␈↓language.␈α
Namely,␈αthe␈α
S-expression␈α␈↓∧A␈↓␈α
could␈αeither␈α
stand␈α
for␈αitself␈α
or␈αstand␈α
for␈αa␈α
variable␈α␈↓∧A␈↓␈α
whose
␈↓ ↓H␈↓value␈αis␈αwanted.␈α Even␈α␈↓∧(CAR␈αX)␈↓␈αis␈α
ambiguous␈αsince␈αsometime␈αthe␈αS-expression␈α␈↓∧(CAR␈αX)␈↓␈αitself␈α
must
␈↓ ↓H␈↓be␈αreferred␈αto␈αand␈αnot␈αthe␈αresult␈αof␈α
evaluating␈αit.␈α Internal␈αlanguage␈αavoids␈αthe␈αambiguity␈αby␈α
using
␈↓ ↓H␈↓␈↓∧(QUOTE␈α∀␈↓αe␈↓∧)␈↓␈α∀to␈α∃stand␈α∀for␈α∀the␈α∀S-exression␈α∃␈↓αe␈↓.␈α∀ Thus␈α∀the␈α∀publication␈α∃language␈α∀form␈α∀␈↓βa␈α∃␈↓∧(A␈α∀B)␈↓
␈↓ ↓H␈↓corresponds␈α⊂to␈α⊂the␈α⊃internal␈α⊂language␈α⊂form␈α⊃␈↓∧(CAR␈α⊂(QUOTE␈α⊂(A.B)))␈↓␈α⊂and␈α⊃both␈α⊂have␈α⊂the␈α⊃value␈α⊂␈↓∧A␈↓
␈↓ ↓H␈↓obtained by taking the ␈↓βa␈↓-part of the S-expression ␈↓∧(A.B)␈↓.
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ I9
␈↓ ↓H␈↓ Since␈α
lists␈α
are␈α∞ a␈α
particular␈α
kind␈α
of␈α∞ S-expression,␈α
the␈α
meanings␈α
of␈α∞ ␈↓βa␈↓␈α
and␈α
␈↓βd␈↓␈α∞ for␈α
lists
␈↓ ↓H␈↓are also determined by the above definitions. Thus, we have
␈↓ ↓H␈↓ ␈↓βa␈↓∧(A B C D) ␈↓↓=␈↓β A
␈↓ ↓H␈↓β␈↓and
␈↓ ↓H␈↓ ␈↓βd␈↓∧(A B C D) ␈↓↓= ␈↓∧(B C D),
␈↓ ↓H␈↓∧␈↓and␈αwe␈αsee␈αthat,␈αin␈αgeneral,␈α ␈↓βa␈α␈↓αx␈↓␈α is␈αthe␈αfirst␈αelement␈αof␈α the␈α list␈α ␈↓αx␈↓,␈αand␈α ␈↓βd␈α␈↓αx␈↓␈α is␈αthe␈αrest␈αof␈αthe␈αlist,
␈↓ ↓H␈↓deleting that first element.
␈↓ ↓H␈↓ Notice␈α
that␈α
for␈α
S-expressions,␈α
the␈α
definitions␈α
of␈α
␈↓βa␈α
␈↓αx␈↓␈α
and␈α
␈↓βd␈α
␈↓αx␈↓␈α
are␈α
symmetrical,␈α
while␈α
for
␈↓ ↓H␈↓lists␈α∞the␈α∞symmetry␈α∞is␈α∞lost.␈α
This␈α∞is␈α∞because␈α∞ of␈α∞ the␈α
unsymmetrical␈α∞nature␈α∞of␈α∞the␈α∞convention␈α
that
␈↓ ↓H␈↓defines lists in terms of S-expressions.
␈↓ ↓H␈↓ Notice,␈α
further,␈α
our␈α
convention␈α
of␈α∞ writing␈α
␈↓βa␈↓␈α
and␈α
␈↓βd␈↓ ␈α
without␈α∞ brackets␈α
surrounding
␈↓ ↓H␈↓the␈αargument.␈α
Also,␈αwe␈α
use␈αlower␈αcase␈α
letters␈αfor␈α
our␈αfunction␈αnames␈α
and␈αfor␈α
variables,␈αreserving
␈↓ ↓H␈↓the upper case letters for the S-expressions themselves.
␈↓ ↓H␈↓ The␈α third␈α
function␈α ␈↓αcons␈↓↓[␈↓αx␈↓↓,␈α␈↓αy␈↓↓]␈↓␈α
forms␈αthe␈αS-expression␈α
whose␈αa-part␈αand␈α
d-part␈αare␈α ␈↓αx␈↓␈α
and
␈↓ ↓H␈↓␈↓αy␈↓ respectively. Thus
␈↓ ↓H␈↓ ␈↓αcons␈↓↓[␈↓∧(A.B), A␈↓↓] = ␈↓∧((A.B).A).
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓We␈α⊂ see␈α⊂ that␈α⊂ for␈α⊂ lists,␈α⊂ ␈↓αcons␈↓␈α⊂ is␈α⊂a␈α⊂prefixing␈α⊂operation.␈α⊂ Namely,␈α⊂ ␈↓αcons␈↓↓[␈↓αx␈↓↓,␈α⊂␈↓αy␈↓↓]␈↓␈α⊂ is␈α⊃the␈α⊂list
␈↓ ↓H␈↓obtained by putting the element ␈↓αx␈↓ onto the front of the list ␈↓αy␈↓. Thus
␈↓ ↓H␈↓ ␈↓αcons␈↓↓[␈↓∧A, (B C D E)␈↓↓] = ␈↓∧(A B C D E).
␈↓ ↓H␈↓∧␈↓If␈αwe␈αwant␈α ␈↓αcons␈↓↓[␈↓αx␈↓↓,␈α␈↓αy␈↓↓]␈↓␈α to␈α be␈α a␈α list,␈α then␈α ␈↓αy␈↓␈α must␈α be␈α a␈α list␈α(possibly␈αthe␈αnull␈αlist␈α ␈↓∧NIL␈↓),␈αand␈α ␈↓αx␈↓
␈↓ ↓H␈↓must␈α
be␈α
a␈αlist␈α
or␈α
an␈αatom.␈α
In␈α
the␈αcase␈α
of␈α
S-expressions␈αin␈α
general,␈α
there␈αis␈α
no␈α
restriction␈α
on␈α the
␈↓ ↓H␈↓arguments of ␈↓αcons␈↓. Usually, we shall write ␈↓αx . y␈↓ instead of ␈↓αcons␈↓↓[␈↓αx␈↓↓, ␈↓αy␈↓↓]␈↓ since this is briefer.
␈↓ ↓H␈↓ The␈α∞internal␈α∞language␈α∞form␈α∞of␈α∞␈↓αcons[x,y]␈↓␈α∞is␈α∞␈↓∧(CONS␈α∞X␈α∞Y)␈↓.␈α∞ The␈α∞name␈α∞"CONS"␈α∂comes␈α∞from
␈↓ ↓H␈↓"construct" referring to the way ␈↓αcons[x,y]␈↓ is constructed from the free storage list.
␈↓ ↓H␈↓ The␈α first␈α
predicate␈α is␈α
␈↓βat␈α␈↓αx␈↓.␈α
␈↓βat␈α␈↓αx␈↓␈α
is␈α true␈α
if␈α the␈α
S-expression␈α ␈↓αx␈↓␈α
is␈αatomic␈α
and␈αfalse
␈↓ ↓H␈↓otherwise. The internal form is ␈↓∧(ATOM X)␈↓.
␈↓ ↓H␈↓ The␈α∀ second␈α∀ predicate␈α∀ is␈α∀equality␈α∀of␈α∀atomic␈α∪symbols␈α∀written␈α∀␈↓αx␈α∀␈↓βeq␈α∀␈↓αy␈↓.␈α∀Equality␈α∀of␈α∪S-
␈↓ ↓H␈↓expressions␈α∂is␈α∞tested␈α∂by␈α∞a␈α∂ program␈α∞ based␈α∂ on␈α∞␈↓βeq␈↓.␈α∂ Actually␈α∞ ␈↓βeq␈↓␈α∂ does␈α∞a␈α∂bit␈α∞more␈α∂than␈α∞testing
␈↓ ↓H␈↓equality␈αof␈α
atoms.␈α Namely,␈α ␈↓αx␈α
␈↓βeq␈α␈↓αy␈↓␈α
is␈αtrue␈αif␈α
and␈αonly␈α
if␈αthe␈αaddresses␈α
of␈α the␈α
first␈αwords␈α of␈α
the
␈↓ ↓H␈↓S-expressions␈α ␈↓αx␈↓␈α and␈α ␈↓αy␈↓␈α are␈αequal.␈α Therefore,␈αif␈α␈↓αx␈α␈↓βeq␈α␈↓αy␈↓,␈αthen␈α ␈↓αx␈↓␈α and␈α␈↓αy␈↓␈αare␈αcertainly␈α the␈α same
␈↓ ↓H␈↓S-expression␈α since␈αthey␈α are␈α represented␈αby␈αthe␈αsame␈αstructure␈αin␈αmemory.␈α The␈αconverse␈αis␈αfalse
␈↓ ↓H␈↓because␈αthere␈αis␈αno␈α guarantee␈α in␈α general␈α that␈α the␈α same␈αS-expression␈αis␈αnot␈αrepresented␈αby␈αtwo
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :10
␈↓ ↓H␈↓different␈αlist␈αstructures.␈α In␈αthe␈αparticular␈αcase␈αwhere␈αat␈αleast␈αone␈αof␈αthe␈αS-expressions␈αis␈α known␈αto
␈↓ ↓H␈↓be␈α
an␈α
atom,␈α
this␈αguarantee␈α
can␈α
be␈α
given,␈α
because␈αLISP␈α
represents␈α
atoms␈α
uniquely␈α
in␈αmemory.␈α
The
␈↓ ↓H␈↓internal form is ␈↓∧(EQ X Y)␈↓.
␈↓ ↓H␈↓ The␈αabove␈αare␈αall␈α
the␈αbasic␈αfunctions␈αof␈α
LISP;␈αall␈αother␈αLISP␈α
functions␈α can␈α be␈α built␈α
from
␈↓ ↓H␈↓them␈α∞ using␈α∞ recursive␈α
conditional␈α∞expressions␈α∞as␈α
will␈α∞shortly␈α∞be␈α
explained.␈α∞However,␈α∞the␈α∞use␈α
of
␈↓ ↓H␈↓certain abbreviations makes LISP programs easier to write and understand.
␈↓ ↓H␈↓ ␈↓βn␈α
␈↓αx␈↓␈α
is␈α an␈α
abbreviation␈α
for␈α ␈↓αx␈α
␈↓βeq␈α
␈↓∧NIL␈↓.␈α
It␈αrates␈α
a␈α
special␈αnotation␈α
because␈α
␈↓∧NIL␈↓␈α
plays␈αthe
␈↓ ↓H␈↓same␈α∞ role␈α∞ among␈α∂ lists␈α∞ that␈α∞ zero␈α∞plays␈α∂ among␈α∞ numbers,␈α∞ and␈α∞list␈α∂variables␈α∞often␈α∞have␈α∂to␈α∞be
␈↓ ↓H␈↓tested to see if their value is ␈↓∧NIL␈↓. Its internal form is ␈↓∧(NULL X)␈↓.
␈↓ ↓H␈↓ The␈α
notation␈α
␈↓αlist␈↓↓[␈↓αx1,␈αx2,␈α
.␈α
.␈α.␈α
,␈α
xn␈↓↓]␈↓␈αis␈α
used␈α
to␈α
denote␈αthe␈α
composition␈α
of␈α␈↓αcons␈↓'s␈α
that␈α
forms␈αa␈α
list
␈↓ ↓H␈↓from its elements. Thus
␈↓ ↓H␈↓ ␈↓αlist[x, y, z] = cons[x, cons[y, cons[z, ␈↓∧NIL␈↓α]]]␈↓
␈↓ ↓H␈↓and␈αforms␈αa␈αlist␈α
of␈αthree␈αelements␈αout␈αof␈α
the␈αquantities␈α␈↓αx,␈αy,␈α␈↓␈α
and␈α␈↓αz␈↓.␈α We␈αoften␈αwrite␈α
␈↓↓<␈↓αx␈αy␈α.␈α.␈α.␈α
z␈↓↓>␈↓
␈↓ ↓H␈↓instead␈αof␈α␈↓αlist␈↓↓[␈↓αx,␈αy,␈α.␈α .␈α.␈α,␈αz␈↓↓]␈↓␈αwhen␈αthis␈αwill␈αnot␈αcause␈αconfusion.␈α The␈αexperienced␈αimplementer
␈↓ ↓H␈↓of␈α⊂programming␈α⊃languages␈α⊂will␈α⊃expect␈α⊂that␈α⊃since␈α⊂␈↓αlist␈↓␈α⊂has␈α⊃a␈α⊂variable␈α⊃number␈α⊂of␈α⊃arguments,␈α⊂its
␈↓ ↓H␈↓implementation␈αwill␈αpose␈αproblems.␈α He␈αwill␈αbe␈αright.␈α The␈αinternal␈αform␈αof␈α␈↓α<x␈αy␈α...␈αz>␈↓␈αis␈α␈↓∧(LIST␈αX
␈↓ ↓H␈↓∧Y ... Z)␈↓.
␈↓ ↓H␈↓ Compositions␈αof␈α␈↓βa␈↓␈αand␈α␈↓βd␈↓␈αare␈αwritten␈αby␈α
concatenating␈αthe␈αletters␈α␈↓βa␈↓␈αand␈α␈↓βd␈↓.␈α Thus,␈αit␈α
is␈αeasily
␈↓ ↓H␈↓seen␈αthat␈α␈↓βad␈α␈↓αx␈↓␈αdenotes␈αthe␈αsecond␈αelement␈αof␈αthe␈αlist␈α␈↓αx␈↓,␈αand␈α␈↓βadd␈α␈↓αx␈↓␈αdenotes␈αthe␈αthird␈αelement␈αof␈α
that
␈↓ ↓H␈↓list. The internal forms of these functions are ␈↓∧CADR␈↓ and ␈↓∧CADDR␈↓ respectively.
␈↓ ↓H␈↓ Besides␈αthe␈αbasic␈αfunctions␈αof␈αLISP,␈αthere␈αwill␈αbe␈αuser-defined␈αfunctions.␈α We␈αhaven't␈αgiven
␈↓ ↓H␈↓the␈α∞mechanism␈α∞function␈α∂definition␈α∞yet,␈α∞but␈α∂suppose␈α∞a␈α∞function␈α∂␈↓αsubst␈↓␈α∞taking␈α∞three␈α∂arguments␈α∞has
␈↓ ↓H␈↓been defined. It may be used in forms like ␈↓αsubst[x,y,z]␈↓ having internal form ␈↓∧(SUBST X Y Z)␈↓.
␈↓ ↓H␈↓ As␈α∀in␈α∪other␈α∀programming␈α∪languages␈α∀and␈α∪in␈α∀mathematics␈α∪generally,␈α∀new␈α∪forms␈α∀can␈α∪be
␈↓ ↓H␈↓constructed␈αby␈αapplying␈αthe␈αfunctions␈αand␈αpredicates␈α
to␈αother␈αforms␈αand␈αnot␈αjust␈αto␈α
variables␈αand
␈↓ ↓H␈↓constants.␈α⊃ Thus␈α⊂we␈α⊃have␈α⊂forms␈α⊃like␈α⊂␈↓αsubst[x,y,␈↓βa␈α⊃z␈↓α]␈α⊂.␈α⊃subst[x,y,␈↓βd␈α⊂␈↓αz]␈↓␈α⊃involving␈α⊂a␈α⊃user␈α⊂defined
␈↓ ↓H␈↓function ␈↓αsubst␈↓. Its internal form is ␈↓∧(CONS (SUBST X Y (CAR Z)) (SUBST X Y (CDR Z)))␈↓.
␈↓ ↓H␈↓6. ␈↓βConditional expressions.␈↓
␈↓ ↓H␈↓ When␈αthe␈αform␈αthat␈αrepresents␈αa␈αfunction␈αdepends␈αon␈αwhether␈αa␈αcertain␈α predicate␈αis␈αtrue␈αof
␈↓ ↓H␈↓the␈α∪arguments,␈α∩conditional␈α∪expressions.␈α∩ are␈α∪used.␈α∩ The␈α∪conditional␈α∩expression␈α∪(more␈α∩properly
␈↓ ↓H␈↓␈↓αconditional form␈↓)
␈↓ ↓H␈↓ ␈↓βif ␈↓αp ␈↓βthen ␈↓αa ␈↓βelse ␈↓αb
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :11
␈↓ ↓H␈↓α␈↓is␈α∞evaluated␈α
as␈α∞follows:␈α
First␈α∞ ␈↓αp␈↓␈α
is␈α∞evaluated␈α∞and␈α
determined␈α∞to␈α
be␈α∞true␈α
or␈α∞false.␈α
If␈α∞ ␈↓αp␈↓␈α∞ is␈α
true,
␈↓ ↓H␈↓then␈α ␈↓αa␈↓␈α is␈αevaluated␈αand␈α its␈α value␈αis␈α the␈α value␈α of␈α the␈α expression.␈α If␈α ␈↓αp␈↓␈α is␈αfalse,␈αthen␈α ␈↓αb␈↓␈α is
␈↓ ↓H␈↓evaluated␈α∩and␈α⊃its␈α∩value␈α⊃is␈α∩the␈α⊃value␈α∩of␈α∩the␈α⊃expression.␈α∩ Note␈α⊃that␈α∩if␈α⊃␈↓αp␈↓␈α∩ is␈α⊃true,␈α∩ ␈↓αb␈↓␈α∩ is␈α⊃never
␈↓ ↓H␈↓evaluated,␈αand␈α
if␈α ␈↓αp␈↓␈α
is␈αfalse,␈α
then␈α ␈↓αa␈↓␈α
is␈αnever␈α
evaluated.␈α The␈α
importance␈αof␈α
this␈αwill␈αbe␈α
explained
␈↓ ↓H␈↓later.␈α∞ A␈α∞familiar␈α∞ function␈α∂that␈α∞can␈α∞be␈α∞written␈α∂conveniently␈α∞using␈α∞conditional␈α∞expressions␈α∂is␈α∞the
␈↓ ↓H␈↓absolute value of a real number. We have
␈↓ ↓H␈↓ ␈↓↓|␈↓αx␈↓↓| = ␈↓βif ␈↓αx␈↓↓<␈↓∧0 ␈↓βthen ␈↓↓-␈↓αx ␈↓βelse ␈↓αx.
␈↓ ↓H␈↓α␈↓A more general form of conditional expression is
␈↓ ↓H␈↓ ␈↓βif ␈↓αp ␈↓βthen ␈↓αa ␈↓βelse if ␈↓αq ␈↓βthen ␈↓αb . . . ␈↓βelse if ␈↓αs ␈↓βthen ␈↓αd ␈↓βelse ␈↓αe.
␈↓ ↓H␈↓α␈↓There␈α can␈α be␈α any␈α number␈α of␈α terms;␈α the␈α value␈α is␈αdetermined␈αby␈αevaluating␈αthe␈αpropositional
␈↓ ↓H␈↓terms␈α ␈↓αp,␈α q␈↓,␈αetc.␈αuntil␈αa␈αtrue␈αterm␈α is␈αfound;␈α the␈αvalue␈αis␈αthen␈αthat␈αof␈αthe␈αterm␈αfollowing␈αthe␈αnext
␈↓ ↓H␈↓␈↓βthen␈↓.␈α
If␈α
none␈α
of␈α
the␈α
propositional␈α
terms␈α
is␈α
true,␈α
then␈α
the␈α
value␈α
is␈α
that␈α
of␈α
the␈α
term␈α∞following␈α
the
␈↓ ↓H␈↓␈↓βelse␈↓.
␈↓ ↓H␈↓ The function graphed in figure 4 is described by the equation
␈↓ ↓H␈↓ ␈↓αtri␈↓↓[␈↓αx␈↓↓] = ␈↓βif ␈↓αx␈↓↓<-␈↓∧1 ␈↓βthen ␈↓∧0 ␈↓βelse if ␈↓αx␈↓↓<␈↓∧0 ␈↓βthen ␈↓∧1␈↓↓+␈↓αx ␈↓βelse if ␈↓αx␈↓↓<␈↓∧1 ␈↓βthen ␈↓∧1␈↓↓-␈↓αx ␈↓βelse ␈↓∧0.
␈↓"␈↓ ↓H␈↓␈αλ ~
␈↓"␈↓ ↓H␈↓␈αλ ~
␈↓"␈↓ ↓H␈↓ ≤'`≥(0,1)␈↓ ↓H␈αλ ~
␈↓"␈↓ ↓H␈↓ ≤'␈αλ ~␈αλ `≥
␈↓"␈↓ ↓H␈↓ ≤'␈αλ ~␈αλ `≥
␈↓"␈↓ ↓H␈↓ ≤'␈αλ ~␈αλ `≥
␈↓"␈↓ ↓H␈↓ '␈αλ ~␈αλ `␈↓ ↓H ======================αααααααααααααααααα======================
␈↓"␈↓ ↓H␈↓␈αλ (-1,0) ~ (1,0)
␈↓"␈↓ ↓H␈↓␈αλ ~
␈↓"␈↓ ↓H␈↓␈αλ ~
␈↓"␈↓ ↓H␈↓ Figure 4.
␈↓ ↓H␈↓∧␈↓ ␈↓∧The␈α
internal␈α∞form␈α
of␈α∞conditional␈α
forms␈α
is␈α∞made␈α
in␈α∞a␈α
more␈α
regular␈α∞way␈α
than␈α∞the␈α
publication
␈↓ ↓H␈↓∧form; the publication form was altered to conform to ALGOL. We write
␈↓ ↓H␈↓∧␈↓ ␈↓∧(COND (p1 e1) (p2 e2) ... (pn en))␈↓
␈↓ ↓H␈↓with␈α
any␈α
number␈α
of␈α
terms.␈α
Its␈α
value␈α
is␈α
determined␈α
by␈α
evaluating␈α
the␈α
␈↓∧p␈↓'s␈α
successively␈α
until␈α
one␈αis
␈↓ ↓H␈↓found␈αwhich␈α
is␈αtrue.␈α Then␈α
the␈αcorresponding␈α␈↓∧e␈↓␈α
is␈αtaken␈α
as␈αthe␈αvalue␈α
of␈αthe␈αconditional␈α
expression.
␈↓ ↓H␈↓If␈αnone␈αof␈αthe␈α
␈↓∧p␈↓'s␈αis␈αtrue,␈αthen␈α
the␈αvalue␈αof␈αthe␈αconditional␈α
expression␈αis␈αundefined.␈α Thus␈α
all␈αthe
␈↓ ↓H␈↓␈↓∧e␈↓'s␈α⊗are␈α↔treated␈α⊗the␈α↔same␈α⊗which␈α⊗makes␈α↔programs␈α⊗for␈α↔interpreting␈α⊗or␈α↔compiling␈α⊗conditional
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :12
␈↓ ↓H␈↓expressions␈α
easier␈α
to␈α
write.␈α
Putting␈α
parentheses␈α
around␈α
each␈α
␈↓∧p-e␈↓␈α
pair␈α
was␈α
probably␈α
a␈α
mistake␈αin
␈↓ ↓H␈↓the␈α
design␈αof␈α
LISP,␈αbecause␈α
it␈α
unnecessarily␈αincreases␈α
the␈αnumber␈α
of␈αright␈α
parentheses.␈α
It␈αshould
␈↓ ↓H␈↓have␈αbeen␈α ␈↓∧(COND␈αp1␈αe1␈αp2␈αe2␈α...␈αpn␈αen)␈↓,␈αbut␈αsuch␈αa␈αchange␈αshould␈αbe␈αmade␈αnow␈αonly␈αas␈αpart␈αof␈αa
␈↓ ↓H␈↓massive improvement.
␈↓ ↓H␈↓ Conditional expressions may be compounded with functions to get forms like
␈↓ ↓H␈↓ ␈↓βif n ␈↓αx ␈↓βthen ␈↓∧NIL ␈↓βelse a ␈↓αx . append[␈↓βd ␈↓αx,y]␈↓
␈↓ ↓H␈↓involving a yet to be defined function ␈↓αappend␈↓. The internal form of this is
␈↓ ↓H␈↓␈↓∧(COND ((NULL X) NIL) (T (CONS (CAR X) (APPEND (CDR X) Y))))␈↓.
␈↓ ↓H␈↓One␈αwould␈αnormally␈αhave␈αexpected␈αto␈αwrite␈α␈↓∧(QUOTE␈αNIL)␈↓␈αand␈α␈↓∧(QUOTE␈αT)␈↓,␈αbut␈αthere␈αis␈αa␈αspecial
␈↓ ↓H␈↓convention␈α
that␈α
␈↓∧NIL␈↓␈αand␈α
␈↓∧T␈↓␈α
may␈α
be␈αwritten␈α
without␈α
␈↓∧QUOTE␈↓.␈α
This␈αmeans␈α
that␈α
these␈αsymbols␈α
cannot
␈↓ ↓H␈↓be␈αused␈α
as␈αvariables.␈α The␈α
value␈αof␈α␈↓∧T␈↓␈α
is␈αthe␈α
propositional␈αconstant␈α␈↓βtrue␈↓,␈α
i.e.␈αit␈αis␈α
always␈αtrue␈αso␈α
that
␈↓ ↓H␈↓it␈α∂is␈α⊂impossible␈α∂to␈α⊂"fall␈α∂off␈α∂the␈α⊂end"␈α∂of␈α⊂a␈α∂conditional␈α∂expression␈α⊂with␈α∂␈↓∧T␈↓␈α⊂as␈α∂its␈α∂last␈α⊂␈↓∧p␈↓.␈α∂ It␈α⊂is␈α∂the
␈↓ ↓H␈↓translation into internal form of the final ␈↓βelse␈↓ of a conditional expression.
␈↓ ↓H␈↓7. ␈↓βBoolean expressions.␈↓
␈↓ ↓H␈↓ In␈α∩ making␈α∩ up␈α∪ the␈α∩ propositional␈α∩ parts␈α∩ of␈α∪ conditional␈α∩expressions,␈α∩ it␈α∪ is␈α∩ often
␈↓ ↓H␈↓necessary␈α∂ to␈α∞ combine␈α∂ elementary␈α∂propositional␈α∞expressions␈α∂using␈α∞the␈α∂Boolean␈α∂operators␈α∞ and,
␈↓ ↓H␈↓or,␈α
and␈α
not.␈α
We␈α
use␈α the␈α
symbols␈α
␈↓↓∧␈↓,␈α
␈↓↓∨␈↓,␈α
and␈α ␈↓↓¬␈↓␈α
respectively,␈α
for␈α
these␈α
operators.␈α The␈α
Boolean
␈↓ ↓H␈↓operators␈αmay␈αbe␈αdescribed␈αsimply␈αby␈αlisting␈αthe␈αvalues␈αof␈αthe␈αelementary␈αBoolean␈αexpressions␈αfor
␈↓ ↓H␈↓each case of the arguments. Thus we have:
␈↓ ↓H␈↓ ␈↓∧T␈↓↓∧␈↓∧T ␈↓↓= ␈↓∧T
␈↓ ↓H␈↓∧␈↓ ␈↓∧T␈↓↓∧␈↓∧F ␈↓↓= ␈↓∧F
␈↓ ↓H␈↓∧␈↓ ␈↓∧F␈↓↓∧␈↓∧T ␈↓↓= ␈↓∧F
␈↓ ↓H␈↓∧␈↓ ␈↓∧F␈↓↓∧␈↓∧F ␈↓↓= ␈↓∧F
␈↓ ↓H␈↓∧␈↓ ␈↓∧T␈↓↓∨␈↓∧T ␈↓↓= ␈↓∧T
␈↓ ↓H␈↓∧␈↓ ␈↓∧T␈↓↓∨␈↓∧F ␈↓↓= ␈↓∧T
␈↓ ↓H␈↓∧␈↓ ␈↓∧F␈↓↓∨␈↓∧T ␈↓↓= ␈↓∧T
␈↓ ↓H␈↓∧␈↓ ␈↓∧F␈↓↓∨␈↓∧F ␈↓↓= ␈↓∧F
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓↓¬␈↓∧T ␈↓↓= ␈↓∧F
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓↓¬␈↓∧F ␈↓↓= ␈↓∧T.
␈↓ ↓H␈↓∧␈↓ ␈↓∧Since␈α⊂both␈α∂∧␈α⊂and␈α⊂∨␈α∂are␈α⊂associative␈α⊂we␈α∂can␈α⊂write␈α⊂Boolean␈α∂forms␈α⊂like␈α⊂␈↓αp1∧p2∧p3␈↓␈α∂without
␈↓ ↓H␈↓worrying␈α
about␈αgrouping.␈α
The␈α
internal␈αforms␈α
are␈α␈↓∧(AND␈α
p1␈α
p2␈α...␈α
pn)␈↓,␈α␈↓∧(OR␈α
p1␈α
p2␈α...␈α
pn)␈↓␈α
and␈α␈↓∧(NOT
␈↓ ↓H␈↓∧p)␈↓.␈α It␈αis␈αalso␈αcustomary␈αto␈αuse␈α␈↓∧NIL␈↓␈αinstead␈αof␈α␈↓∧F␈↓,␈αbecause␈αin␈αmost␈αLISP␈αsystems␈αboth␈αBoolean␈α␈↓βfalse␈↓
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :13
␈↓ ↓H␈↓and␈α∩the␈α∩null␈α∩list␈α∩are␈α⊃represented␈α∩by␈α∩the␈α∩number␈α∩0.␈α⊃ This␈α∩makes␈α∩certain␈α∩tests␈α∩run␈α∩faster,␈α⊃but
␈↓ ↓H␈↓introduces so many complications in implementation that it was certainly a bad idea.
␈↓ ↓H␈↓ The␈α∞ Boolean␈α∂ operators␈α∞ can␈α∞ be␈α∂ described␈α∞ by␈α∞ conditional␈α∂expressions␈α∞in␈α∂the␈α∞following
␈↓ ↓H␈↓way:
␈↓ ↓H␈↓ ␈↓αp␈↓↓∧␈↓αq ␈↓↓= ␈↓βif ␈↓αp ␈↓βthen ␈↓αq ␈↓βelse ␈↓∧F
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓αp␈↓↓∨␈↓αq ␈↓↓= ␈↓βif ␈↓αp ␈↓βthen ␈↓∧T ␈↓βelse ␈↓αq
␈↓ ↓H␈↓α␈↓ ␈↓α␈↓↓¬␈↓αp ␈↓↓= ␈↓βif ␈↓αp ␈↓βthen ␈↓∧F ␈↓βelse ␈↓∧T.
␈↓ ↓H␈↓∧␈↓Note␈αthat␈αif␈α ␈↓αp␈↓␈α is␈αfalse␈α ␈↓αp␈↓↓∧␈↓αq␈↓␈α is␈αfalse␈αindependent␈αof␈αthe␈αvalue␈α of␈α␈↓αq␈↓,␈α and␈α likewise␈α if␈α ␈↓αp␈↓␈α is␈αtrue,
␈↓ ↓H␈↓then␈α∞ ␈↓αp␈↓↓∨␈↓αq␈↓␈α∂ is␈α∞true␈α∞independent␈α∂of␈α∞␈↓αq␈↓.␈α∂ If␈α∞ ␈↓αp␈↓␈α∞ has␈α∂been␈α∞evaluated␈α∂and␈α∞found␈α∞to␈α∂be␈α∞false,␈α∂then␈α∞ ␈↓αq␈↓
␈↓ ↓H␈↓does␈α∂not␈α∂ have␈α⊂ to␈α∂ be␈α∂evaluated␈α⊂at␈α∂all␈α∂to␈α⊂find␈α∂the␈α∂value␈α⊂of␈α∂ ␈↓αp␈↓↓∧␈↓αq␈↓,␈α∂and,␈α⊂in␈α∂fact,␈α∂LISP␈α⊂does␈α∂not
␈↓ ↓H␈↓evaluate␈α
␈↓αq␈↓␈α
in␈αthis␈α
case.␈α
Similarly,␈α ␈↓αq␈↓␈α
is␈α
not␈α
evaluated␈α in␈α
evaluating␈α
␈↓αp␈↓↓∨␈↓αq␈↓␈α if␈α
␈↓αp␈↓␈α
is␈α
true.␈αThis
␈↓ ↓H␈↓procedure␈α⊂is␈α⊃in␈α⊂accordance␈α⊃with␈α⊂the␈α⊂above␈α⊃conditional␈α⊂expression␈α⊃descriptions␈α⊂of␈α⊃ the␈α⊂Boolean
␈↓ ↓H␈↓operators.␈α The␈αimportance␈αof␈α
this␈αconvention␈αwhich␈αcontrasts␈αwith␈α
that␈αof␈αALGOL␈α 60,␈α
will␈α be
␈↓ ↓H␈↓apparent later when we discuss recursive definitions of functions and predicates.
␈↓ ↓H␈↓ Boolean␈α
expressions␈α
can␈α
be␈α
combined␈α
with␈α
functions␈α
and␈α
conditional␈α
exrpessions␈α
to␈α
get␈α
forms
␈↓ ↓H␈↓like
␈↓ ↓H␈↓ ␈↓β if n ␈↓αx ∨ ␈↓βn d ␈↓α x ␈↓βthen ␈↓αx ␈↓βelse a ␈↓αx . alt ␈↓βdd ␈↓αx␈↓
␈↓ ↓H␈↓the internal form of which is
␈↓ ↓H␈↓ ␈↓∧(COND ((OR (NULL X) (NULL (CDR X))) X) (T (CONS (CAR X) (ALT (CDDR X)))))␈↓.
␈↓ ↓H␈↓8. ␈↓βRecursive function definitions.␈↓
␈↓ ↓H␈↓ In␈α∞such␈α
languages␈α∞as␈α
FORTRAN␈α∞and␈α
Algol,␈α∞ computer␈α
progrrams␈α∞are␈α
expressed␈α∞ in␈α
the
␈↓ ↓H␈↓main␈αas␈αsequences␈α
of␈αassignment␈αstatements␈αand␈α
conditional␈α␈↓βgo␈αto␈↓'s.␈α
In␈αLISP,␈αprograms␈αare␈α
mainly
␈↓ ↓H␈↓expressed in the form of recursively defined functions. We begin with an example.
␈↓ ↓H␈↓ The␈α∞ function␈α∞ ␈↓αalt␈↓↓[␈↓αx␈↓↓]␈↓␈α∞ gives␈α∞ a␈α∞ list␈α∂ whose␈α∞ elements␈α∞ are␈α∞alternate␈α∞elements␈α∞of␈α∞the␈α∂list␈α∞ ␈↓αx␈↓
␈↓ ↓H␈↓beginning with the first. Thus
␈↓ ↓H␈↓ ␈↓αalt␈↓↓[␈↓∧(A B C D E)␈↓↓] = ␈↓∧(A C E),
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓αalt␈↓↓[␈↓∧(((A B) (C D))␈↓↓] = ␈↓∧((A B)),
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓αalt␈↓↓[␈↓∧(A)␈↓↓] = ␈↓∧(A),
␈↓ ↓H␈↓∧␈↓and
␈↓ ↓H␈↓ ␈↓αalt␈↓↓[␈↓∧NIL␈↓↓] = ␈↓∧NIL.
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :14
␈↓ ↓H␈↓∧ In LISP ␈↓αalt␈↓ is defined by the equation
␈↓ ↓H␈↓ ␈↓αalt[x] ← ␈↓βif n ␈↓αx ∨ ␈↓βn d ␈↓αx ␈↓βthen ␈↓αx ␈↓βelse a ␈↓αx . alt[␈↓βdd ␈↓αx␈↓↓]␈↓.
␈↓ ↓H␈↓In case you need a review of our precedence conventions, fully bracketed it looks like
␈↓ ↓H␈↓ ␈↓αalt[x] ← ␈↓βif ␈↓α[␈↓βn␈↓α[x] ∨ ␈↓βn␈↓α[␈↓βd␈↓α[x]]] ␈↓βthen␈↓α x ␈↓βelse␈↓α [␈↓βa␈↓α[x] . alt[␈↓βdd␈↓α[x]]]␈↓.
␈↓ ↓H␈↓ This␈α
definition␈α
uses␈αno␈α
ways␈α
of␈αforming␈α
expressions␈α
that␈αwe␈α
haven't␈α
introduced␈αpreviously,
␈↓ ↓H␈↓but␈α
it␈α
uses␈α
the␈α
function␈α
␈↓αalt␈↓␈α
in␈α
the␈α
right␈α
hand␈α
side␈α
of␈α
its␈α
own␈α
defining␈α
equation.␈α
To␈α
get␈α
the␈αvalue␈α
of
␈↓ ↓H␈↓␈↓αalt␈αx␈↓␈αfor␈αsome␈αparticular␈αlist,␈αwe␈αevaluate␈αthe␈αright␈αhand␈αside␈αof␈αthe␈αdefinition␈αsubstituting␈αthat␈α
list
␈↓ ↓H␈↓for␈α␈↓αx␈↓␈αwhenever␈α
␈↓αx␈↓␈αis␈αencountered␈α
and␈αre-evaluating␈αthe␈αright␈α
hand␈αside␈αwith␈α
a␈αnew␈α␈↓αx␈↓␈α
whenever␈αa
␈↓ ↓H␈↓form ␈↓αalt e␈↓ is encountered.
␈↓ ↓H␈↓ This process is best explained by using it to evaluate some expressions.
␈↓ ↓H␈↓ Consider␈α
evaluating␈α
␈↓αalt[␈↓∧NIL␈↓↓]␈↓.␈α
We␈α
do␈α
it␈α
by␈α
evaluating␈α
the␈α
expression␈α
to␈α
the␈α
right␈α
of␈α
the␈α ␈↓↓←␈↓
␈↓ ↓H␈↓sign␈α
remembering␈α
that␈α
the␈α
variable␈α
␈↓αx␈↓␈α
has␈α
the␈α
value␈α
␈↓∧NIL␈↓.␈α
A␈α
conditional␈α
expression␈α is␈α
evaluated
␈↓ ↓H␈↓by␈α
first␈α∞evaluating␈α
the␈α
first␈α∞propositional␈α
term␈α
¬␈α∞in␈α
this␈α
case␈α∞ ␈↓βn␈α
␈↓αx␈α
␈↓↓∨␈α∞␈↓βn␈α
d␈α
␈↓αx␈↓.␈α∞This␈α
expression␈α∞is␈α
a
␈↓ ↓H␈↓disjunction␈αand␈αin␈α its␈αevaluation␈αwe␈αfirst␈αevaluate␈αthe␈αfirst␈αdisjunct,␈αnamely␈α ␈↓βn ␈↓αx␈↓.␈α Since␈α ␈↓αx␈α␈↓↓=␈α␈↓∧NIL,
␈↓ ↓H␈↓∧␈↓βn␈α␈↓αx␈↓␈α is␈αtrue,␈αthe␈αdisjunction␈αis␈αtrue,␈αand␈αthe␈αvalue␈αof␈α the␈α conditional␈α expression␈α is␈α the␈αvalue␈αof
␈↓ ↓H␈↓the␈α∞term␈α
after␈α∞the␈α
first␈α∞true␈α
propositiional␈α∞term.␈α∞The␈α
term␈α∞is␈α
␈↓αx␈↓,␈α∞ and␈α
its␈α∞ value␈α
is␈α∞␈↓∧NIL␈↓,␈α∞ so␈α
the
␈↓ ↓H␈↓value␈α
of␈α
␈↓αalt␈↓↓[␈↓∧NIL␈↓↓]␈↓␈α∞ is␈α
␈↓∧NIL␈↓␈α
as␈α
stated␈α∞above.␈α
Obeying␈α
the␈α
rules␈α∞about␈α
the␈α
order␈α
of␈α∞evaluation␈α
of
␈↓ ↓H␈↓terms␈α∞in␈α∂ conditional␈α∞ and␈α∂Boolean␈α∞expressions␈α∂is␈α∞important␈α∂in␈α∞this␈α∂case␈α∞since␈α∂if␈α∞we␈α∂didn't␈α∞obey
␈↓ ↓H␈↓these␈αrules,␈αwe␈αmight␈αfind␈αourselves␈αtrying␈αto␈α evaluate␈α ␈↓βn␈αd␈α␈↓αx␈↓␈α or␈α␈↓αalt␈↓↓[␈↓βdd␈α␈↓αx␈↓↓]␈↓,␈αand␈αsince␈α ␈↓αx␈↓␈α
is␈α ␈↓∧NIL␈↓,
␈↓ ↓H␈↓neither␈α∞of␈α∞these␈α
has␈α∞a␈α∞value.␈α∞ An␈α
attempt␈α∞to␈α∞evaluate␈α
them␈α∞in␈α∞LISP␈α∞would␈α
give␈α∞rise␈α∞to␈α∞an␈α
error
␈↓ ↓H␈↓return.
␈↓ ↓H␈↓ As␈α
a␈αsecond␈α
example,␈αconsider␈α
␈↓αalt␈↓↓[␈↓∧(A␈α
B)␈↓↓]␈↓.␈α Since␈α
neither␈α␈↓βn␈α
␈↓αx␈↓␈αnor␈α
␈↓βn␈α
d␈α␈↓αx␈↓␈α
is␈αtrue␈α
in␈αthis␈α
case,
␈↓ ↓H␈↓the␈αdisjunction␈α␈↓βn␈α␈↓αx␈α␈↓↓∨␈α␈↓βn␈αd␈α␈↓αx␈α␈↓is␈α false␈α and␈α the␈α value␈α of␈α the␈α expression␈α is␈α the␈α value␈α of␈α␈↓βa␈α␈↓αx␈α
.
␈↓ ↓H␈↓αalt␈↓↓[␈↓βdd␈α␈↓αx␈↓↓].␈α ␈↓βa␈α␈↓αx␈↓␈αhas␈αthe␈αvalue␈α ␈↓∧A␈↓,␈αand␈α␈↓βdd␈α␈↓αx␈↓␈αhas␈αthe␈αvalue␈α␈↓∧NIL␈↓,␈αso␈αwe␈αmust␈αnow␈αevaluate␈α ␈↓αalt␈↓↓[␈↓∧NIL␈↓↓]␈↓,
␈↓ ↓H␈↓and␈α∞we␈α∞already␈α∞know␈α∞that␈α
the␈α∞value␈α∞ of␈α∞ this␈α∞ expression␈α
is␈α∞ ␈↓∧NIL␈↓.␈α∞ Therefore,␈α∞ the␈α∞ value␈α
of
␈↓ ↓H␈↓␈↓αalt␈↓↓[␈↓∧(A B)␈↓↓]␈↓ is that of ␈↓∧A.NIL␈↓ which is ␈↓∧(A)␈↓.
␈↓ ↓H␈↓ We can describe this evaluation in a less wordy way by writing
␈↓ ↓H␈↓ ␈↓αalt␈↓↓[␈↓∧(A B)␈↓↓] = ␈↓βif n␈↓∧(A B) ␈↓↓∨ ␈↓βn d␈↓∧(A B) ␈↓βthen ␈↓∧(A B) ␈↓βelse
␈↓ ↓H␈↓β a␈↓∧(A B).␈↓αalt␈↓↓[␈↓βdd␈↓∧(A B)␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓βif ␈↓∧NIL ␈↓↓∨ ␈↓βn d␈↓∧(A B) ␈↓βthen ␈↓∧(A B) ␈↓βelse
␈↓ ↓H␈↓β a␈↓∧(A B).␈↓αalt␈↓↓[␈↓βdd␈↓∧(A B)␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓βif ␈↓∧NIL ␈↓βthen ␈↓∧(A B) ␈↓βelse
␈↓ ↓H␈↓β a␈↓∧(A B).␈↓αalt␈↓↓[␈↓βdd␈↓∧(A B)␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓βa␈↓∧(A B).␈↓αalt␈↓↓[␈↓βdd␈↓∧(A B)␈↓↓]
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :15
␈↓ ↓H␈↓↓ = ␈↓∧A.␈↓αalt␈↓↓[␈↓∧NIL␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓∧A␈↓↓.[␈↓βif n␈↓∧NIL ␈↓↓∨ ␈↓βnd␈↓∧NIL ␈↓βthen ␈↓∧NIL ␈↓βelse
␈↓ ↓H␈↓β a␈↓∧NIL.␈↓αalt␈↓↓[␈↓βdd␈↓∧NIL␈↓↓]]
␈↓ ↓H␈↓↓ = ␈↓∧A␈↓↓.[␈↓βif ␈↓∧T ␈↓↓∨ ␈↓βnd␈↓∧NIL ␈↓βthen ␈↓∧NIL ␈↓βelse
␈↓ ↓H␈↓β a␈↓∧NIL.␈↓αalt␈↓↓[␈↓βdd␈↓∧NIL␈↓↓]]
␈↓ ↓H␈↓↓ = ␈↓∧A␈↓↓.[␈↓βif ␈↓∧T ␈↓βthen ␈↓∧NIL ␈↓βelse
␈↓ ↓H␈↓β a␈↓∧NIL.␈↓αalt␈↓↓[␈↓βdd␈↓∧NIL␈↓↓]]
␈↓ ↓H␈↓↓ = ␈↓∧A␈↓↓.[␈↓∧NIL␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓∧(A).
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓This␈α⊂is␈α⊂still␈α⊂very␈α⊂ long-winded,␈α⊂ and␈α⊂ now␈α⊂ that␈α⊂ the␈α⊂ reader␈α⊂understands␈α⊂ the␈α⊃ order␈α⊂ of
␈↓ ↓H␈↓evaluation of conditional and Boolean expressions, we can proceed more briefly to evaluate
␈↓ ↓H␈↓ ␈↓αalt␈↓↓[␈↓∧(A B C D E)␈↓↓] = ␈↓∧A.␈↓αalt␈↓↓[␈↓∧(C D E)␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓∧A␈↓↓.[␈↓∧C.␈↓αalt␈↓↓[␈↓∧(E)␈↓↓]]
␈↓ ↓H␈↓↓ = ␈↓∧A.␈↓↓[␈↓∧C.(E)␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓∧(A C E).
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓Here␈α
are␈α
three␈α
more␈α
examples␈α
of␈αrecursive␈α
functions␈α
and␈α
their␈α
application:␈α
We␈α
define␈α ␈↓αlast␈↓
␈↓ ↓H␈↓by
␈↓ ↓H␈↓ ␈↓αlast␈↓↓[␈↓αx␈↓↓] ← ␈↓βif n d ␈↓αx ␈↓βthen a ␈↓αx ␈↓βelse ␈↓αlast␈↓↓[␈↓βd ␈↓αx␈↓↓],
␈↓ ↓H␈↓↓␈↓and we compute
␈↓ ↓H␈↓ ␈↓αlast␈↓↓[␈↓∧(A B C)␈↓↓] = ␈↓βif nd␈↓∧(A B C) ␈↓βthen a␈↓∧(A B C) ␈↓βelse ␈↓αlast␈↓↓[␈↓βd␈↓∧(A B C)␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓αlast␈↓↓[␈↓∧(B C)␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓αlast␈↓↓[␈↓∧(C)␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓∧C.
␈↓ ↓H␈↓∧␈↓Clearly␈α
␈↓αlast␈↓␈α
computes␈α the␈α
last␈α
element␈αof␈α
a␈α
list.␈α ␈↓αlast␈↓↓[␈↓∧NIL␈↓↓]␈↓␈α
is␈α
undefined␈αin␈α
the␈α
LISP␈αlanguage;
␈↓ ↓H␈↓the␈α⊃result␈α⊂of␈α⊃trying␈α⊂ to␈α⊃ compute␈α⊂ it␈α⊃may␈α⊂be␈α⊃an␈α⊂error␈α⊃message␈α⊂or␈α⊃may␈α⊂be␈α⊃some␈α⊃random␈α⊂result
␈↓ ↓H␈↓depending on the implementation.
␈↓ ↓H␈↓ The function ␈↓αsubst␈↓ is defined by
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :16
␈↓ ↓H␈↓ ␈↓αsubst␈↓↓[␈↓αx, y, z␈↓↓] ← ␈↓βif at ␈↓αz ␈↓βthen ␈↓↓[␈↓βif ␈↓αz ␈↓βeq ␈↓αy ␈↓βthen ␈↓αx ␈↓βelse ␈↓αz␈↓↓] ␈↓βelse ␈↓αsubst[x,y,␈↓βa ␈↓αz].subst[x,y,␈↓βd ␈↓αz]␈↓.
␈↓ ↓H␈↓We have
␈↓ ↓H␈↓ ␈↓αsubst␈↓↓[␈↓∧(A.B), X, ((X.A).X)␈↓↓] =
␈↓ ↓H␈↓↓␈↓ ␈↓↓= ␈↓αsubst␈↓↓[␈↓∧(A.B), X, (X.A)␈↓↓]␈↓αsubst␈↓↓[␈↓∧(A.B), X, X␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓= [␈↓αsubst␈↓↓[␈↓∧(A.B), X, X␈↓↓].␈↓αsubst␈↓↓[␈↓∧(A.B), X, A␈↓↓]].␈↓∧(A.B)
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓↓= [[␈↓∧(A.B)␈↓↓].␈↓∧A␈↓↓].␈↓∧(A.B)␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓= ␈↓∧(((A.B).A).(A.B)).
␈↓ ↓H␈↓∧␈↓αsubst␈↓␈α∩ computes␈α∪the␈α∩result␈α∪of␈α∩substituting␈α∪the␈α∩S-expression␈α∪ ␈↓αx␈↓␈α∩ for␈α∪the␈α∩ atom␈α∪ ␈↓αy␈↓␈α∩ in␈α∪the␈α∩S-
␈↓ ↓H␈↓expression ␈↓αz␈↓. This operation is important in all kinds of symbolic computation.
␈↓ ↓H␈↓ The␈α∩function␈α∩␈↓αappend[x,y]␈↓␈α∩which␈α∩gives␈α∩the␈α∪ concatenation␈α∩ of␈α∩the␈α∩lists␈α∩␈↓αx␈↓␈α∩and␈α∩␈↓αx␈↓␈α∪is␈α∩also
␈↓ ↓H␈↓important.␈α It␈αis␈α
also␈αdenoted␈αby␈α
the␈αinfixed␈αexpression␈α
␈↓αx␈↓↓*␈↓αy␈↓␈α since␈αit␈α
is␈αassociative.␈α We␈α
have␈αthe
␈↓ ↓H␈↓examples
␈↓ ↓H␈↓ ␈↓∧(A B C)␈↓↓*␈↓∧(D E F) ␈↓↓= ␈↓∧(A B C D E F),
␈↓ ↓H␈↓∧␈↓and
␈↓ ↓H␈↓ ␈↓∧NIL␈↓↓*␈↓∧(A B) ␈↓↓= ␈↓∧(A B) ␈↓↓= ␈↓∧(A B)␈↓↓*␈↓∧NIL,
␈↓ ↓H␈↓∧␈↓and the formal definition
␈↓ ↓H␈↓ ␈↓αx␈↓↓*␈↓αy ␈↓↓← ␈↓βif n ␈↓αx ␈↓βthen ␈↓αy ␈↓βelse ␈↓↓[␈↓βa ␈↓αx].[[␈↓βd ␈↓αx␈↓↓]*␈↓αy␈↓↓].
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓The␈α∂ Boolean␈α∂ operations␈α∂can␈α∞also␈α∂be␈α∂used␈α∂in␈α∞making␈α∂recursive␈α∂definitions␈α∂ provided␈α∞ we
␈↓ ↓H␈↓observe the order of evaluation of constituents. First, we define a predicate ␈↓αequal␈↓ by
␈↓ ↓H␈↓ ␈↓αequal[x,y] ← x ␈↓βeq ␈↓αy ∨ [¬␈↓βat ␈↓αx ∧ ¬␈↓βat ␈↓αy ∧ equal[␈↓βa ␈↓αx,␈↓βa ␈↓αy] ∧ equal[␈↓βd ␈↓αx, ␈↓βd ␈↓αy]]␈↓.
␈↓ ↓H␈↓␈↓αequal[x,y]␈↓␈αis␈αtrue␈αif␈αand␈αonly␈αif␈α ␈↓αx␈↓␈α and␈α ␈↓αy␈↓␈α are␈α the␈α same␈αS-expression,␈α and␈α the␈α use␈α of␈αthis
␈↓ ↓H␈↓predicate␈αmakes␈αup␈αfor␈αthe␈αfact␈αthat␈α
the␈αbasic␈αpredicate␈α ␈↓βeq␈↓␈α is␈αguaranteed␈α to␈α test␈α
equality␈α only
␈↓ ↓H␈↓when one of the operands is known to be an atom. We shall also use the infixes ␈↓↓=␈↓ and ␈↓↓≠␈↓.
␈↓ ↓H␈↓ Membership of an S-expression ␈↓αx␈↓ in a list ␈↓αy␈↓ is tested by
␈↓ ↓H␈↓ ␈↓αmember␈↓↓[␈↓αx, y␈↓↓] ← ¬␈↓βn ␈↓αy ␈↓↓∧ [[␈↓αx ␈↓↓= ␈↓βa ␈↓αy␈↓↓] ∨ ␈↓αmember␈↓↓[␈↓αx, ␈↓βd ␈↓αy␈↓↓]].
␈↓ ↓H␈↓↓␈↓This relation is also denoted by ␈↓αx ε y␈↓. Her? are some computations:
␈↓ ↓H␈↓ ␈↓αmember␈↓↓[␈↓∧B, (A B)␈↓↓] = ¬␈↓βn ␈↓∧(A B) ␈↓↓∧ [[␈↓∧B ␈↓↓= ␈↓βa ␈↓∧(A B)␈↓↓]
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :17
␈↓ ↓H␈↓↓ ∨ ␈↓αmember␈↓↓[␈↓∧B, ␈↓βd ␈↓∧(A B)␈↓↓]],
␈↓ ↓H␈↓↓ = ␈↓αmember␈↓↓[␈↓∧B, (B)␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓∧T.
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓Sometimes␈α a␈α function␈α is␈α
defined␈αwith␈αthe␈αhelp␈αof␈α
auxiliary␈αfunctions.␈α Thus,␈αthe␈αreverse␈α
of
␈↓ ↓H␈↓a list ␈↓αx␈↓ , (e.g. ␈↓αreverse␈↓↓[␈↓∧(A B C D)␈↓↓] = ␈↓∧(D C B A)␈↓), is given by
␈↓ ↓H␈↓ ␈↓αreverse␈↓↓[␈↓αx␈↓↓] ← ␈↓αrev␈↓↓[␈↓αx, ␈↓∧NIL␈↓↓]
␈↓ ↓H␈↓↓␈↓where
␈↓ ↓H␈↓ ␈↓αrev␈↓↓[␈↓αx, y␈↓↓] ← ␈↓βif n ␈↓αx ␈↓βthen ␈↓αy ␈↓βelse ␈↓αrev␈↓↓[␈↓βd ␈↓αx␈↓↓, [␈↓βa ␈↓αx␈↓↓].␈↓αy␈↓↓].
␈↓ ↓H␈↓↓␈↓A computation is
␈↓ ↓H␈↓ ␈↓αreverse␈↓↓[␈↓∧(A B C)␈↓↓] = ␈↓αrev␈↓↓[␈↓∧(A B C), NIL␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓αrev␈↓↓[␈↓∧(B C), (A)␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓αrev␈↓↓[␈↓∧(C), (B A)␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓αrev␈↓↓[␈↓∧NIL, (C B A)␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓∧(C B A).
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓A more elaborate example of recursive definition is given by
␈↓ ↓H␈↓ ␈↓αflatten␈↓↓[␈↓αx␈↓↓] ← ␈↓αflat␈↓↓[␈↓αx, ␈↓∧NIL␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓αflat␈↓↓[␈↓αx, y␈↓↓] ← ␈↓βif at ␈↓αx ␈↓βthen ␈↓αx.y
␈↓ ↓H␈↓α ␈↓βelse ␈↓αflat␈↓↓[␈↓βa ␈↓αx, flat␈↓↓[␈↓βd ␈↓αx, y␈↓↓]].
␈↓ ↓H␈↓↓␈↓We have
␈↓ ↓H␈↓ ␈↓αflatten␈↓↓[␈↓∧((A.B).C)␈↓↓] = ␈↓αflat␈↓↓[␈↓∧((A.B).C), NIL␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓αflat␈↓↓[␈↓∧(A.B), ␈↓αflat␈↓↓[␈↓∧C, NIL␈↓↓]]
␈↓ ↓H␈↓↓ = ␈↓αflat␈↓↓[␈↓∧(A.B), (C)␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓αflat␈↓↓[␈↓∧A, ␈↓αflat␈↓↓[␈↓∧B, (C)␈↓↓]]
␈↓ ↓H␈↓↓ = ␈↓αflat␈↓↓[␈↓∧A, (B C)␈↓↓]
␈↓ ↓H␈↓↓ = ␈↓∧(A B C).
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :18
␈↓ ↓H␈↓∧␈↓The␈α reader␈α
will␈αsee␈αthat␈α
the␈αvalue␈α
of␈α ␈↓αflatten␈↓↓[␈↓αx␈↓↓]␈↓␈α is␈α
a␈αlist␈α
of␈αthe␈αatoms␈α
of␈α the␈α
S-expression␈α ␈↓αx␈↓
␈↓ ↓H␈↓from left to write. Thus ␈↓αflatten␈↓↓[␈↓∧((A B) A)␈↓↓] = ␈↓∧(A B NIL A NIL)␈↓.
␈↓ ↓H␈↓ Many␈α⊃ functions␈α⊃ can␈α⊃be␈α⊃conveniently␈α⊃written␈α⊃in␈α⊃more␈α⊃than␈α⊃one␈α⊃way.␈α⊃ For␈α⊃example,␈α⊂the
␈↓ ↓H␈↓function ␈↓αreverse␈↓ mentioned above can be written without an auxiliary function as follows:
␈↓ ↓H␈↓ ␈↓αreverse␈↓↓[␈↓αx␈↓↓] ← ␈↓βif n ␈↓αx ␈↓βthen ␈↓∧NIL ␈↓βelse ␈↓αreverse␈↓↓[␈↓βd ␈↓αx␈↓↓]*␈↓∧<a x>
␈↓ ↓H␈↓∧␈↓but it will be explained later that the earlier definition involves less computation.
␈↓ ↓H␈↓ The␈α∞use␈α∞of␈α∞conditional␈α
expressions␈α∞ for␈α∞ recursive␈α∞ function␈α
definition␈α∞ is␈α∞ not␈α∞ limited␈α
to
␈↓ ↓H␈↓functions␈α of␈α S-expressions.␈α
For␈αexample,␈αthe␈α
factorial␈αfunction␈αand␈α
the␈αEuclidean␈αalgorithm␈α
for
␈↓ ↓H␈↓the greatest common divisor are expressed as follows:
␈↓ ↓H␈↓ ␈↓αn␈↓↓! ← ␈↓βif ␈↓αn␈↓↓=␈↓∧0 ␈↓βthen ␈↓∧1 ␈↓βelse ␈↓αn␈↓↓(␈↓αn␈↓↓-␈↓∧1␈↓↓)!
␈↓ ↓H␈↓↓␈↓and
␈↓ ↓H␈↓ ␈↓αgcd␈↓↓(␈↓αm, n␈↓↓) ← ␈↓βif ␈↓αm␈↓↓>␈↓αn ␈↓βthen ␈↓αgcd␈↓↓(␈↓αn, m␈↓↓) ␈↓βelse if ␈↓αm␈↓↓=␈↓∧0 ␈↓βthen ␈↓αn
␈↓ ↓H␈↓α ␈↓βelse ␈↓αgcd␈↓↓(␈↓αn mod m, m␈↓↓)
␈↓ ↓H␈↓↓␈↓where␈α
␈↓αn␈α
mod␈α
m␈↓␈α
denotes␈αthe␈α
remainder␈α
when␈α
␈↓αn␈↓␈α
is␈α
divided␈αby␈α
␈↓αm␈↓␈α
and␈α
may␈α
itself␈α
be␈αexpressed
␈↓ ↓H␈↓recursively by
␈↓ ↓H␈↓ ␈↓αn mod m ␈↓↓← ␈↓βif ␈↓αn␈↓↓<␈↓αm ␈↓βthen ␈↓αn ␈↓βelse ␈↓↓(␈↓αn␈↓↓-␈↓αm␈↓↓) ␈↓αmod m.␈↓
␈↓ ↓H␈↓ The␈α∞internal␈α∞form␈α∂of␈α∞function␈α∞definitions␈α∞depends␈α∂on␈α∞the␈α∞implementation.␈α∂ Stanford␈α∞LISP
␈↓ ↓H␈↓and UCI LISP for the PDP-10 computer use the form
␈↓ ↓H␈↓ ␈↓∧(DE <function name> <list of variables> <right hand side>)␈↓.
␈↓ ↓H␈↓In these LISPs, the above definition of ␈↓αsubst␈↓ is
␈↓ ↓H␈↓ ␈↓∧(DE␈αSUBST␈α(X␈αY␈αZ)␈α(COND␈α((ATOM␈αZ)␈α(COND␈α((EQ␈αZ␈αX)␈αY)␈α(T␈αZ)))␈α(T␈α(CONS␈α(SUBST␈αX
␈↓ ↓H␈↓∧Y (CAR Z)) (SUBST X Y (CDR Z))))))␈↓,
␈↓ ↓H␈↓and the definition of ␈↓αalt␈↓ is
␈↓ ↓H␈↓ ␈↓∧(DE␈αALT␈α(X)␈α(COND␈α((OR␈α(NULL␈αX)␈α(NULL␈α(CDR␈αX)))␈αX)␈α(T␈α(CONS␈α(CAR␈αX)␈α(ALT␈α(CDDR
␈↓ ↓H␈↓∧X))))))␈↓.
␈↓ ↓H␈↓ Another␈α⊂notation␈α⊂for␈α⊃function␈α⊂definition␈α⊂called␈α⊂the␈α⊃␈↓∧DEFPROP␈↓␈α⊂notation␈α⊂will␈α⊃be␈α⊂explained
␈↓ ↓H␈↓after λ-expressions have been introduced.
␈↓ ↓H␈↓ Exercises
␈↓ ↓H␈↓ 1. Consider the function ␈↓αdrop␈↓ defined by
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :19
␈↓ ↓H␈↓ ␈↓αdrop␈↓↓[␈↓αx␈↓↓] ← ␈↓βif n ␈↓αx ␈↓βthen ␈↓∧NIL ␈↓βelse ␈↓↓<␈↓βa ␈↓αx␈↓↓>.␈↓αdrop␈↓↓[␈↓βd ␈↓αx␈↓↓].
␈↓ ↓H␈↓↓␈↓Compute␈α∂(by␈α∂hand)␈α∂ ␈↓αdrop␈↓↓[␈↓∧(A␈α∂B␈α⊂C)␈↓↓]␈↓.␈α∂ What␈α∂does␈α∂ ␈↓αdrop␈↓␈α∂ do␈α⊂ to␈α∂ lists␈α∂ in␈α∂general?␈α∂ Write␈α⊂␈↓αdrop␈↓␈α∂in
␈↓ ↓H␈↓internal notation using ␈↓∧DE␈↓.
␈↓ ↓H␈↓ 2. What does the function
␈↓ ↓H␈↓ ␈↓αr2␈↓↓[␈↓αx␈↓↓] ← ␈↓βif n ␈↓αx ␈↓βthen ␈↓∧NIL ␈↓βelse ␈↓αreverse␈↓↓[␈↓βa ␈↓αx␈↓↓].␈↓αr2␈↓↓[␈↓βd ␈↓αx␈↓↓]
␈↓ ↓H␈↓↓␈↓do to lists of lists? How about
␈↓ ↓H␈↓ ␈↓αr3␈↓↓[␈↓αx␈↓↓] ← ␈↓βif at ␈↓αx ␈↓βthen ␈↓αx ␈↓βelse ␈↓αreverse␈↓↓[␈↓αr4␈↓↓[␈↓αx␈↓↓]]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓αr4␈↓↓[␈↓αx␈↓↓] ← ␈↓βif n ␈↓αx ␈↓βthen ␈↓∧NIL ␈↓βelse ␈↓αr3␈↓↓[␈↓βa ␈↓αx␈↓↓].␈↓αr4␈↓↓[␈↓βd ␈↓αx␈↓↓]?
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓3. Compare
␈↓ ↓H␈↓ ␈↓αr3'␈↓↓[␈↓αx␈↓↓] ← ␈↓βif at ␈↓αx ␈↓βthen ␈↓αx ␈↓βelse ␈↓αr3'␈↓↓[␈↓βd ␈↓αx␈↓↓]*<␈↓αr3'␈↓↓[␈↓βa ␈↓αx␈↓↓]>
␈↓ ↓H␈↓↓␈↓with the function ␈↓αr3␈↓ of the preceding example.
␈↓ ↓H␈↓ 4. Consider ␈↓αr5␈↓ defined by
␈↓ ↓H␈↓ ␈↓αr5␈↓↓[␈↓αx␈↓↓] ← ␈↓βif n ␈↓αx ␈↓↓∨ ␈↓βn d ␈↓αx ␈↓βthen ␈↓αx
␈↓ ↓H␈↓α␈↓ ␈↓α␈↓βelse ␈↓↓[␈↓βa ␈↓αr5␈↓↓[␈↓βd ␈↓αx␈↓↓]]
␈↓ ↓H␈↓↓ . ␈↓αr5␈↓↓[␈↓βa ␈↓αx . r5␈↓↓[␈↓βd ␈↓αr5␈↓↓[␈↓βd ␈↓αx␈↓↓]]].
␈↓ ↓H␈↓↓␈↓Compute␈α ␈↓αr5␈↓↓[␈↓∧(A␈αB␈αC␈αD)␈↓↓]␈↓.␈α What␈αdoes␈α ␈↓∧r5␈↓␈α do␈αin␈αgeneral?␈α Needless␈α to␈αsay,␈αthis␈αis␈αnot␈αa␈αgood␈αway
␈↓ ↓H␈↓of computing this function even though it involves no auxiliary functions.
␈↓ ↓H␈↓9. ␈↓βLambda expressions and functions with functions as arguments.␈↓
␈↓ ↓H␈↓ It␈α
is␈α
common␈α
to␈α
use␈α
phrases␈α
like␈α
"the␈α
function␈α
2␈↓αx␈↓↓+␈↓αy␈↓".␈α
This␈α
is␈α
not␈α
a␈α
precise␈αnotation␈α
because
␈↓ ↓H␈↓we␈α∃cannot␈α∃say␈α∃ 2␈↓αx␈↓↓+␈↓αy␈↓↓(␈↓∧3,␈α∃4␈↓↓)␈↓␈α∃ and␈α∃know␈α∀whether␈α∃the␈α∃desired␈α∃ result␈α∃ is␈α∃ 2␈↓
␈␈↓3+4␈α∃ or␈α∀ 2␈↓
␈␈↓4+3
␈↓ ↓H␈↓regarding␈α the␈αexpression␈α as␈αa␈αfunction␈αof␈αtwo␈αvariables.␈α Worse␈αyet,␈αwe␈αmight␈αhave␈αmeant␈αa␈αone-
␈↓ ↓H␈↓variable function of ␈↓αx␈↓ wherein ␈↓αy␈↓ is regarded as a parameter.
␈↓ ↓H␈↓ The␈α∂problem␈α∂ of␈α∂ giving␈α∂ names␈α∂ to␈α∞ functions␈α∂ is␈α∂ solved␈α∂ by␈α∂Church's␈α∂ λ-notation.␈α∞ In
␈↓ ↓H␈↓the␈α∂ above␈α∂ example,␈α∂ we␈α∂ would␈α∂ write␈α∂␈↓↓λ␈↓αx␈α⊂y␈↓↓:␈α∂␈↓∧2␈↓αx␈↓↓+␈↓αy␈↓␈α∂ to␈α∂denote␈α∂ the␈α∂ function␈α∂ of␈α⊂ two␈α∂ variables
␈↓ ↓H␈↓with␈α first␈αargument␈α ␈↓αx␈↓␈α and␈α second␈α argument␈α
␈↓αy␈↓␈α whose␈αvalue␈αis␈αgiven␈αby␈αthe␈αexpression␈α
2␈↓αx␈↓↓+␈↓αy␈↓.
␈↓ ↓H␈↓Thus,
␈↓ ↓H␈↓ ␈↓↓[λ␈↓αx y␈↓↓: ␈↓∧2␈↓αx␈↓↓+␈↓αy␈↓↓][␈↓∧3, 4␈↓↓] = ␈↓∧10␈↓.
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :20
␈↓ ↓H␈↓Likewise,␈α∩ ␈↓↓[λ␈↓αy␈α⊃x␈↓↓:␈α∩␈↓∧2␈↓αx␈↓↓+␈↓αy␈↓↓][␈↓∧3,␈α⊃4␈↓↓]␈α∩=␈α⊃␈↓∧11␈↓.␈α∩ Like␈α⊃variables␈α∩of␈α⊃integration␈α∩and␈α⊃the␈α∩bound␈α∩variables␈α⊃of
␈↓ ↓H␈↓quantifiers␈αin␈α
logic,␈αvariables␈α
following␈α λ␈α
are␈α bound␈α
or␈αdummy␈α
and␈αthe␈α
expression␈αas␈α
a␈αwhole
␈↓ ↓H␈↓may␈α
be␈α∞replaced␈α
by␈α
any␈α∞others␈α
provided␈α
the␈α∞replacement␈α
is␈α
done␈α∞consistently␈α
and␈α
does␈α∞not␈α
make
␈↓ ↓H␈↓any␈α∂ variable␈α∞ bound␈α∂ by␈α∂ λ␈α∞ the␈α∂same␈α∂as␈α∞a␈α∂free␈α∂variable␈α∞in␈α∂the␈α∂expression.␈α∞ Thus␈α∂ ␈↓↓λ␈↓αx␈α∂y␈↓↓:␈α∞␈↓∧2␈↓αx␈↓↓+␈↓αy␈↓
␈↓ ↓H␈↓represents␈α the␈α same␈α
function␈α as␈α␈↓↓λ␈↓αy␈αx␈↓↓:␈α
␈↓∧2␈↓αy␈↓↓+␈↓αx␈↓␈αor␈α␈↓↓λ␈↓αu␈αv␈↓↓:␈α
␈↓∧2␈↓αu␈↓↓+␈↓αv␈↓␈α,␈αbut␈αin␈α
the␈αfunction␈αof␈αone␈α
argument
␈↓ ↓H␈↓␈↓↓λ␈↓αx␈↓↓: ␈↓∧2␈↓αx␈↓↓+␈↓αy␈↓ , we cannot replace the variable ␈↓αx␈↓ by ␈↓αy␈↓ , though we could replace it by ␈↓αu␈↓.
␈↓ ↓H␈↓ λ-notation␈αplays␈αtwo␈αimportant␈α roles␈α in␈α LISP.␈α First,␈α it␈αallows␈αus␈αto␈αrewrite␈αan␈αexpression
␈↓ ↓H␈↓containing␈αtwo␈αor␈αmore␈αoccurrences␈αof␈αthe␈αsame␈αsub-expression␈αin␈αsuch␈αa␈αway␈αthat␈αthe␈α expression
␈↓ ↓H␈↓occurs␈α∪only␈α∪once.␈α∪Thus␈α∪ ␈↓↓(␈↓∧2␈↓αx␈↓↓+␈↓∧1␈↓↓)␈↓ε4␈↓↓+␈↓∧3␈↓↓(␈↓∧2␈↓αx␈↓↓+␈↓∧1␈↓↓)␈↓ε3␈↓␈α∪ can␈α∪be␈α∪written␈α∪␈↓↓[λ␈↓αw␈↓↓:␈α∪␈↓αw␈↓ε4␈↓↓+␈↓∧3␈↓αw␈↓ε3␈↓↓][␈↓∧2␈↓αx␈↓↓+␈↓∧1␈↓↓]␈↓.␈α∪This␈α∀can␈α∪save
␈↓ ↓H␈↓considerable␈αcomputation,␈α
and␈αcorresponds␈α
to␈αthe␈αpractice␈α
in␈αordinary␈α
programming␈αof␈αassigning␈α
to
␈↓ ↓H␈↓a␈αvariable␈αthe␈αvalue␈αof␈αa␈αsub-expression␈αthat␈αoccurs␈αmore␈αthan␈αonce␈α in␈αan␈α expression␈α and␈α then
␈↓ ↓H␈↓writing the expression in terms of the variable.
␈↓ ↓H␈↓ The␈α∞second␈α∞use␈α∞of␈α∞λ-expressions␈α∞is␈α∂in␈α∞ using␈α∞ functions␈α∞ that␈α∞take␈α∞functions␈α∂as␈α∞arguments.
␈↓ ↓H␈↓Suppose␈αwe␈α
want␈αto␈αform␈α
a␈αnew␈αlist␈α
from␈αan␈αold␈α
one␈αby␈αapplying␈α
a␈αfunction␈α ␈↓αf␈↓␈α
to␈αeach␈αelement␈α
of
␈↓ ↓H␈↓the list. This can be done using the function ␈↓αmapcar␈↓ defined by
␈↓ ↓H␈↓ ␈↓αmapcar␈↓↓[␈↓αx, f␈↓↓] ← ␈↓βif n ␈↓αx ␈↓βthen ␈↓∧NIL ␈↓βelse ␈↓αf␈↓↓[␈↓βa ␈↓αx␈↓↓] . ␈↓αmapcar␈↓↓[␈↓βd ␈↓αx, f␈↓↓].
␈↓ ↓H␈↓↓␈↓Suppose␈α
the␈αoperation␈α
we␈αwant␈α
to␈αperform␈α
is␈α
squaring,␈αand␈α
we␈αwant␈α
to␈αapply␈α
it␈α
to␈αthe␈α
list␈α ␈↓∧(1␈α
2␈α3␈α
4
␈↓ ↓H␈↓∧5 6 7)␈↓. We have
␈↓ ↓H␈↓ ␈↓αmapcar␈↓↓[␈↓∧(1 2 3 4 5 6 7), ␈↓↓λ␈↓αx␈↓↓: ␈↓αx␈↓ε2␈↓↓] = ␈↓∧(1 4 9 16 25 36 49).
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓A␈α
more␈α
generally␈α
useful␈α
operation␈α
than␈α
␈↓αmapcar␈↓␈α
is␈α
␈↓αmaplist␈↓␈α
in␈α
which␈α
the␈α
function␈α
is␈α
applied
␈↓ ↓H␈↓to the successive sublists of the list rather than to the elements. ␈↓αmaplist␈↓ is defined by
␈↓ ↓H␈↓ ␈↓αmaplist␈↓↓[␈↓αx, f␈↓↓] ← ␈↓βif n ␈↓αx ␈↓βthen ␈↓∧NIL ␈↓βelse ␈↓αf␈↓↓[␈↓αx␈↓↓] . ␈↓αmaplist␈↓↓[␈↓βd ␈↓αx, f␈↓↓].
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓As␈α∞ an␈α∞ application␈α∞ of␈α∞ ␈↓αmaplist␈↓␈α∞and␈α∞functional␈α∞arguments,␈α∞we␈α∞shall␈α∞define␈α∞a␈α∂function␈α∞ for
␈↓ ↓H␈↓differentiating␈α algebraic␈α expressions␈αinvolving␈αsums␈αand␈αproducts.␈α The␈αexpressions␈αare␈αbuilt␈αup
␈↓ ↓H␈↓from atoms denoting variables and integer constants according to the syntax
␈↓ ↓H␈↓ <expression> ::= <variable> | <integer> |
␈↓ ↓H␈↓ (PLUS <explist>) | (TIMES <explist>)
␈↓ ↓H␈↓ <explist> ::= <expression> | <expression><explist>
␈↓ ↓H␈↓Here,␈α∂ ␈↓∧PLUS␈↓␈α⊂ followed␈α∂by␈α⊂a␈α∂list␈α⊂of␈α∂arguments␈α⊂denotes␈α∂the␈α⊂sum␈α∂of␈α⊂these␈α∂arguments␈α⊂and␈α∂ ␈↓∧TIMES␈↓
␈↓ ↓H␈↓followed␈α⊂by␈α⊃a␈α⊂list␈α⊂of␈α⊃arguments␈α⊂ denotes␈α⊂ their␈α⊃product.␈α⊂ The␈α⊂ function␈α⊃ ␈↓αdiff␈↓↓[␈↓αe,␈α⊂v␈↓↓]␈↓␈α⊃ gives␈α⊂the
␈↓ ↓H␈↓partial derivative of the expression ␈↓αe␈↓ with respect to the variable ␈↓αv␈↓. We have
␈↓ ↓H␈↓ ␈↓αdiff␈↓↓[␈↓αe, v␈↓↓] ← ␈↓βif at ␈↓αe ␈↓βthen ␈↓↓[␈↓βif ␈↓αe ␈↓βeq ␈↓αv ␈↓βthen ␈↓∧1 ␈↓βelse ␈↓∧0␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe ␈↓βeq ␈↓∧PLUS ␈↓βthen
␈↓ ↓H␈↓β ␈↓∧PLUS . ␈↓αmapcar␈↓↓[␈↓βd ␈↓αe␈↓↓, λ␈↓αx␈↓↓: ␈↓αdiff␈↓↓[␈↓αx, v␈↓↓]_]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe ␈↓βeq ␈↓∧TIMES ␈↓αthen
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :21
␈↓ ↓H␈↓α ␈↓∧PLUS.␈↓αmaplist␈↓↓[␈↓βd ␈↓αe␈↓↓, λ␈↓αx␈↓↓: ␈↓∧TIMES
␈↓ ↓H␈↓∧ . ␈↓αmaplist␈↓↓[␈↓βd ␈↓αe␈↓↓, λ␈↓αy␈↓↓: ␈↓βif ␈↓αx ␈↓βeq ␈↓αy ␈↓βthen
␈↓ ↓H␈↓β ␈↓αdiff␈↓↓[␈↓βa ␈↓αy, v␈↓↓] ␈↓βelse a ␈↓αy␈↓↓]].
␈↓ ↓H␈↓↓␈↓The term that describes the rule for differentiating products corresponds to the rule
␈↓ ↓H␈↓ ␈↓↓∂/∂␈↓αv e␈↓¬i␈↓↓ = [␈↓βif ␈↓αi␈↓↓=␈↓αj ␈↓βthen ␈↓↓∂␈↓αe␈↓¬j␈↓↓/∂␈↓αv ␈↓βelse ␈↓αe␈↓¬j␈↓↓].
␈↓ ↓H␈↓↓and␈α ␈↓αmaplist␈↓␈α has␈α to␈αbe␈αused␈αrather␈α
than␈α ␈↓αmapcar␈↓␈α since␈αwhether␈αto␈αdifferentiate␈αin␈α
forming␈αthe
␈↓ ↓H␈↓product␈αis␈αdetermined␈αby␈αequality␈αof␈αthe␈αindices␈α ␈↓αi␈↓␈α and␈α ␈↓αj␈↓␈α rather␈α than␈αequality␈αof␈αthe␈αterms␈α ␈↓αe␈↓¬i␈↓
␈↓ ↓H␈↓and ␈↓αe␈↓¬j␈↓.
␈↓ ↓H␈↓ Two␈αmore␈αuseful␈αfunctions␈αwith␈αfunctions␈αas␈αarguments␈αare␈αthe␈αpredicates␈α ␈↓αandlis␈↓␈α and␈α ␈↓αorlis␈↓
␈↓ ↓H␈↓defined by the equations
␈↓ ↓H␈↓ ␈↓αandlis␈↓↓[␈↓αu, p␈↓↓] ← ␈↓βn ␈↓αu ␈↓↓∨ [␈↓αp␈↓↓[␈↓βa ␈↓αu␈↓↓] ∧ ␈↓αandlis␈↓↓[␈↓βd ␈↓αu, p␈↓↓]]
␈↓ ↓H␈↓↓␈↓and
␈↓ ↓H␈↓ ␈↓αorlis␈↓↓[␈↓αu, p␈↓↓] ← ¬␈↓βn ␈↓αu ␈↓↓∧ [␈↓αp␈↓↓[␈↓βa ␈↓αu␈↓↓] ∨ ␈↓αorlis␈↓↓[␈↓βd ␈↓αu, p␈↓↓]].
␈↓ ↓H␈↓↓␈↓ ␈↓↓The internal form for a λ-expression is
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓∧(LAMBDA <list of variables> <expression to be evaluated>)␈↓.
␈↓ ↓H␈↓Thus␈α␈↓↓λ␈αx:␈α
diff[x,v]␈↓␈αis␈αwritten␈α␈↓∧(LAMBDA␈α
(X␈αV)␈α(DIFF␈α
X␈αV))␈↓.␈α When␈αa␈α
function␈αspecified␈αwith␈α
λ␈αis
␈↓ ↓H␈↓written␈α⊃as␈α⊃an␈α∩argument␈α⊃of␈α⊃a␈α∩function␈α⊃taking␈α⊃a␈α⊃functional␈α∩argument,␈α⊃then␈α⊃the␈α∩λ-expression␈α⊃is
␈↓ ↓H␈↓marked with the identifier ␈↓∧FUNCTION␈↓. Thus the above definition of ␈↓αdiff␈↓ translates to
␈↓ ↓H␈↓ ␈↓∧(DE DIFF (E V) (COND
␈↓ ↓H␈↓∧␈↓ ␈↓∧((ATOM E) (COND ((EQ E V) 1) (T 0)))
␈↓ ↓H␈↓∧␈↓ ␈↓∧((EQ (CAR E) (QUOTE PLUS))
␈↓ ↓H␈↓∧ (CONS␈α(QUOTE␈αPLUS)␈α(MAPCAR␈α(CDR␈αE)␈α(FUNCTION␈α(LAMBDA␈α(X)␈α(DIFF
␈↓ ↓H␈↓∧X V))))))
␈↓ ↓H␈↓∧␈↓ ␈↓∧((EQ␈α≥(CAR␈α≥E)␈α≥(QUOTE␈α≥TIMES))␈α≥ (CONS␈α≥(QUOTE␈α≥PLUS)
␈↓ ↓H␈↓∧(MAPLIST␈α∪(CDR␈α∀E)␈α∪(FUNCTION␈α∪(LAMBDA␈α∀(X)␈α∪(CONS␈α∪(QUOTE␈α∀TIMES)␈α∪(MAPLIS␈α∀(CDR␈α∪E)
␈↓ ↓H␈↓∧(FUNCTION (LAMBDA (Y) (COND ((EQ X Y) (DIFF (CAR Y) V)) (T (CAR Y))))))))))))))␈↓.
␈↓ ↓H␈↓ Another␈α∞way␈α∞of␈α∞writing␈α∞function␈α∞definitions␈α
in␈α∞internal␈α∞notation␈α∞uses␈α∞␈↓∧LAMBDA␈↓␈α∞to␈α∞make␈α
a
␈↓ ↓H␈↓function of the right side of a definition.
␈↓ ↓H␈↓It is like writing ␈↓αsubst␈↓ and ␈↓αalt␈↓ as
␈↓ ↓H␈↓ ␈↓αsubst = λx y z.[␈↓βif at ␈↓αz ␈↓βthen ␈↓α[␈↓βif ␈↓αz ␈↓βeq␈↓α y ␈↓βthen␈↓α x ␈↓βelse ␈↓αz] ␈↓βelse ␈↓αsubst[x,y,␈↓βa␈↓α z] . subst[x,y,␈↓βd␈↓α z]]␈↓
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :22
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓αalt = λx.[␈↓βif n ␈↓αx ∨ ␈↓βn d ␈↓αx ␈↓βthen␈↓α x ␈↓βelse a ␈↓αx .alt ␈↓βdd␈↓α x]␈↓.
␈↓ ↓H␈↓The definitions of ␈↓αsubst␈↓ and ␈↓αalt␈↓ take the forms
␈↓ ↓H␈↓ ␈↓∧(DEFPROP␈αSUBST␈α(LAMBDA␈α(X␈αY␈αZ)␈α(COND␈α((ATOM␈αZ)␈α(COND␈α((EQ␈αZ␈αX)␈αY)␈α(T␈α
Z)))␈α(T
␈↓ ↓H␈↓∧(CONS (SUBST X Y (CAR Z)) (SUBST X Y (CDR Z)))))))␈↓,
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓∧(DEFPROP␈α⊂ALT␈α⊃(LAMBDA␈α⊂(X)␈α⊃(COND␈α⊂((OR␈α⊃(NULL␈α⊂X)␈α⊃(NULL␈α⊂(CDR␈α⊃X)))␈α⊂X)␈α⊃(T␈α⊂(CONS
␈↓ ↓H␈↓∧(CAR X) (ALT (CDDR X)))))))␈↓.
␈↓ ↓H␈↓ The␈αgeneral␈αform␈α
is␈α␈↓∧(DEFPROP␈α<function␈α
name>␈α<defining␈αλ-expression>)␈↓␈α
and␈αis␈αoften␈α
used
␈↓ ↓H␈↓by programs that output LISP.
␈↓ ↓H␈↓ Exercises
␈↓ ↓H␈↓ 1.␈α
Compute␈α ␈↓αdiff␈↓↓[␈↓∧(TIMES␈α
X␈α
(PLUS␈αY␈α
1)␈α3),␈α
X␈↓↓]␈↓␈α
using␈α the␈α
above␈α
definition␈α of␈α
␈↓αdiff␈↓.␈α Now␈α
do
␈↓ ↓H␈↓you see why algebraic simplification is important?
␈↓ ↓H␈↓ 2. Compute ␈↓αorlis␈↓↓[␈↓∧((A B) (C D) E), ␈↓βat␈↓↓]␈↓.
␈↓ ↓H␈↓10. ␈↓βLabel.␈↓
␈↓ ↓H␈↓ The␈α
λ␈αmechanism␈α
is␈α
not␈α adequate␈α
for␈α
providing␈α names␈α
for␈α
recursive␈α functions,␈α
because
␈↓ ↓H␈↓in␈α∪this␈α∪case␈α∩there␈α∪has␈α∪to␈α∩be␈α∪a␈α∪way␈α∪of␈α∩referring␈α∪to␈α∪the␈α∩function␈α∪name␈α∪within␈α∪the␈α∩ function.
␈↓ ↓H␈↓Therefore,␈α we␈αuse␈α the␈αnotation␈α ␈↓βlabel␈↓↓[␈↓αf,␈αe␈↓↓]␈↓␈α to␈αdenote␈αthe␈αexpression␈α ␈↓αe␈↓␈α but␈αwhere␈αoccurrences␈αof
␈↓ ↓H␈↓␈↓αf␈↓␈α⊃ within␈α⊃ ␈↓αe␈↓␈α⊃ refer␈α⊃ to␈α⊃ the␈α⊃ whole␈α∩ expression.␈α⊃ For␈α⊃example,␈α⊃ suppose␈α⊃we␈α⊃wished␈α⊃to␈α∩define␈α⊃a
␈↓ ↓H␈↓function␈αthat␈α
takes␈αalternate␈αelements␈α
of␈αeach␈α
element␈αof␈αa␈α
list␈αand␈α
makes␈αa␈αlist␈α
of␈αthese.␈α Thus,␈α
we
␈↓ ↓H␈↓want
␈↓ ↓H␈↓ ␈↓αglub␈↓↓[␈↓∧((A B C) (A B C D) (X Y Z))␈↓↓] = ␈↓∧((A C) (A C) (X Z)).␈↓
␈↓ ↓H␈↓We can make the definition
␈↓ ↓H␈↓ ␈↓αglub␈↓↓[␈↓αx␈↓↓]␈α←␈α
␈↓αmapcar␈↓↓[␈↓αx,␈α␈↓βlabel␈↓↓[␈↓αalt␈↓↓,␈αλ␈↓αx␈↓↓:␈α
␈↓βif␈αn␈α
␈↓αx␈α␈↓↓∨␈α␈↓βn␈α
d␈α␈↓αx␈α
␈↓βthen␈α␈↓αx␈α ␈↓βelse␈α
a
␈↓ ↓H␈↓β␈↓αx . alt␈↓↓[␈↓βdd ␈↓αx␈↓↓]]].
␈↓ ↓H␈↓↓␈↓ ␈↓↓The␈α∪internal␈α∪form␈α∪of␈α∪␈↓αlabel[<name>,<function␈α∪expression>]␈↓␈α∪is␈α∪␈↓∧(LABEL␈α∀<name>␈α∪<function
␈↓ ↓H␈↓∧expression>)␈↓, so that the internal form of the definition of ␈↓αglub␈↓ is
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :23
␈↓ ↓H␈↓ ␈↓∧(DE␈α⊃GLUB␈α⊂(X)␈α⊃(MAPCAR␈α⊂X␈α⊃(LABEL␈α⊂ALT␈α⊃(LAMBDA␈α⊂(X)␈α⊃(COND␈α⊂(OR␈α⊃(NULL␈α⊃X)␈α⊂(NULL
␈↓ ↓H␈↓∧(CDR X))) X) (T (CONS (CAR X) (ALT (CDDR X))))))))␈↓.
␈↓ ↓H␈↓ The␈α⊂identifier␈α⊂ ␈↓αalt␈↓␈α⊂ in␈α∂the␈α⊂above␈α⊂example␈α⊂is␈α∂bound␈α⊂by␈α⊂the␈α⊂ label␈α∂ and␈α⊂is␈α⊂ local␈α⊂ to␈α∂ that
␈↓ ↓H␈↓expression,␈α and␈α this␈α
is␈αthe␈αgeneral␈α
rule.␈α The␈αlabel␈α construction␈α
is␈αnot␈αoften␈α
used␈αin␈αLISP␈αsince␈α
it
␈↓ ↓H␈↓is␈α∂more␈α∂usual␈α∂to␈α∂ give␈α∞functions␈α∂global␈α∂definitions.␈α∂D.␈α∂M.␈α∂R.␈α∞Park␈α∂pointed␈α∂out␈α∂that␈α∂if␈α∂we␈α∞allow
␈↓ ↓H␈↓variables␈α∂to␈α∂represent␈α∂functions␈α∂and␈α∞use␈α∂a␈α∂ suitable␈α∂ λ␈α∂construction,␈α∞the␈α∂use␈α∂of␈α∂ label␈α∂ could␈α∞be
␈↓ ↓H␈↓avoided.
␈↓ ↓H␈↓11. ␈↓βNumerical computation.␈↓
␈↓ ↓H␈↓ Numerical␈α
calculation␈α
and␈α
symbolic␈α
calculation␈α
must␈α
often␈α
be␈α
combined,␈α
so␈α
LISP␈α
provides␈α
for
␈↓ ↓H␈↓numerical computation also.
␈↓ ↓H␈↓ In␈α∞the␈α
first␈α∞place,␈α
we␈α∞need␈α
to␈α∞include␈α
numbers␈α∞as␈α
parts␈α∞of␈α
symbolic␈α∞expressions.␈α∞ LISP␈α
has
␈↓ ↓H␈↓both␈α∂integer␈α∞and␈α∂floating␈α∞point␈α∂numbers␈α∞which␈α∂are␈α∞regarded␈α∂as␈α∞atoms.␈α∂ These␈α∞numbers␈α∂may␈α∞be
␈↓ ↓H␈↓included␈α
as␈αatoms␈α
in␈α
writing␈αS-expressions.␈α
Thus␈α
we␈αcan␈α
have␈α
the␈αlists␈α
␈↓∧(1␈α
3␈α5)␈↓,␈α
␈↓∧(3.5␈α
6.1␈α-7.2E9)␈↓,␈α
and
␈↓ ↓H␈↓␈↓∧(PLUS␈αX␈α
1.3)␈↓;␈αthe␈αfirst␈α
is␈αa␈α
list␈αof␈αintegers,␈α
the␈αsecond␈α
a␈αlist␈αof␈α
floating␈αpoint␈α
numbers,␈αand␈αthe␈α
third
␈↓ ↓H␈↓a␈αsymbolic␈αlist␈αcontaining␈αboth␈αnumberical␈αand␈αnon-numerical␈αatoms.␈α Integers␈αare␈αwritten␈αwithout
␈↓ ↓H␈↓decimal␈αpoints␈αwhich␈αare␈αused␈αto␈αsignal␈αfloating␈αpoint␈αnumbers.␈α As␈αin␈αFortran,␈αthe␈αletter␈α␈↓∧E␈↓␈αis␈αused
␈↓ ↓H␈↓to␈αsignal␈αthe␈αexponent␈αof␈αa␈αfloating␈αpoint␈α
number␈αwhich␈αis␈αa␈αsigned␈αinteger.␈α The␈αsizes␈αof␈α
numbers
␈↓ ↓H␈↓admitted␈α∞depends␈α∞on␈α∞the␈α∞implementation.␈α∂ When␈α∞a␈α∞dotted␈α∞pair,␈α∞say␈α∂␈↓∧(1␈α∞.␈α∞2)␈↓␈α∞is␈α∞wanted,␈α∂the␈α∞spaces
␈↓ ↓H␈↓around the dot distinguish it from the list ␈↓∧(1.2)␈↓ whose sole element is the floating point number 1.2.
␈↓ ↓H␈↓ In␈αpublication␈αlanguage␈αwe␈αwill␈αuse␈αordinary␈αmathematical␈αnotation␈αfor␈αnumerical␈αfunctions.
␈↓ ↓H␈↓For␈α
exponentiation␈αwe␈α
will␈αuse␈α
the␈α
usual␈αsuperscript␈α
notation␈α␈↓αx␈↓εy␈↓␈α
when␈α
typographically␈αconvenient
␈↓ ↓H␈↓and␈α
the␈α
linear␈α
Algol␈αnotation␈α
␈↓αx↑y␈↓␈α
when␈α
it␈α
isn't.␈α Of␈α
course,␈α
numerical␈α
and␈α
symbolic␈αcalculation␈α
must
␈↓ ↓H␈↓often be combined, so that the function giving the length of a list can be written
␈↓ ↓H␈↓ ␈↓αlength u ← ␈↓βif n ␈↓αu ␈↓βthen ␈↓∧0 ␈↓βelse ␈↓∧1 ␈↓α+ length ␈↓βd ␈↓αu␈↓.
␈↓ ↓H␈↓ The␈αinternal␈αnotation␈αfor␈αnumerical␈αfunctions␈αis␈αthat␈αused␈αin␈αthe␈αexamples␈αgiven:␈α␈↓∧(PLUS␈αX␈αY
␈↓ ↓H␈↓∧...␈α∞Z)␈↓␈α∞for␈α∞␈↓αx+y+...+z␈↓,␈α∞␈↓∧(TIMES␈α∞X␈α∞...␈α∞Z)␈↓␈α∞for␈α∞␈↓αxy...z␈↓,␈α∞␈↓∧(MINUS␈α∞X)␈↓␈α∞for␈α∞␈↓α-x␈↓,␈α∞␈↓∧(DIFFERENCE␈α∞X␈α∞Y)␈↓␈α∂for␈α∞␈↓αx-y␈↓,
␈↓ ↓H␈↓␈↓∧(QUOTIENT X Y)␈↓ for ␈↓αx/y␈↓, and ␈↓∧(POWER X Y)␈↓ for ␈↓αx␈↓εy␈↓.
␈↓ ↓H␈↓ Since␈α
numbers␈αthat␈α
form␈αpart␈α
of␈αlist␈α
structures␈α
must␈αbe␈α
represented␈αby␈α
pointers␈αanyway,␈α
there
␈↓ ↓H␈↓is␈α∞room␈α∞for␈α
a␈α∞flag␈α∞distinguishing␈α
floating␈α∞point␈α∞numbers␈α
and␈α∞integers.␈α∞ Therefore,␈α∞the␈α
arithmetic
␈↓ ↓H␈↓operations␈αare␈αprogrammed␈αto␈αtreat␈αtypes␈αdynamically,␈αi.e.␈αa␈αvariable␈αmay␈αtake␈αan␈αinteger␈αvalue␈αat
␈↓ ↓H␈↓one␈α∂step␈α∂of␈α∂computation␈α∂and␈α∂a␈α⊂real␈α∂value␈α∂at␈α∂another.␈α∂ The␈α∂subroutines␈α∂realizing␈α⊂the␈α∂arithmetic
␈↓ ↓H␈↓functions␈α
make␈α
the␈αappropriate␈α
tests␈α
and␈αcreate␈α
results␈α
of␈α
appropriate␈αtypes.␈α
This␈α
is␈αslow␈α
compared
␈↓ ↓H␈↓to␈α∂direct␈α∂use␈α∂of␈α∂the␈α∂machine's␈α∂arithmetic␈α⊂instructions,␈α∂so␈α∂that␈α∂LISP␈α∂can␈α∂be␈α∂efficiently␈α⊂used␈α∂only
␈↓ ↓H␈↓when␈αthe␈αnumerical␈α
calculations␈αare␈αsmall␈α
or␈αat␈αleast␈α
small␈αcompared␈αto␈α
the␈αsymbolic␈αcalculations␈α
in
␈↓ ↓H␈↓a problem.
␈↓ ↓H␈↓␈↓ ¬wCHAPTER I␈↓ :24
␈↓ ↓H␈↓ Besides␈αfunctions␈αof␈αnumbers␈αwe␈αneed␈αpredicates␈αon␈αnumbers␈αand␈αthe␈αusual␈α=,␈α<,␈α>,␈α≤,␈αand␈α≥
␈↓ ↓H␈↓are␈α∂used␈α∂with␈α∂the␈α∂internal␈α∞names␈α∂␈↓∧EQUAL,␈α∂LESSP,␈α∂GREATERP,␈α∂LESSEQP␈↓,␈α∂and␈α∞GREATEREQP,
␈↓ ↓H␈↓respectively.␈α Not␈αall␈αare␈αimplemented␈αin␈αall␈αLISP␈αsystems,␈αbut␈αof␈αcourse␈αthe␈αremaining␈αones␈αcan␈α
be
␈↓ ↓H␈↓defined. Besides that, the predicate ␈↓αnumberp␈↓ is used to distinguish numbers from other atoms.
␈↓ ↓H␈↓ It␈α
is␈α
worth␈αremarking␈α
that␈α
including␈αtype␈α
flags␈α
in␈αnumbers␈α
would␈α
benefit␈αmany␈α
programming
␈↓ ↓H␈↓languages besides LISP and would not cost much in either storage or hardware.
␈↓ ↓H␈↓ As␈αa␈αfirst␈αexample␈αof␈αa␈α
combined␈αnumeric␈αand␈αsymbolic␈αcomputation,␈αhere␈αis␈α
an␈αinterpreter
␈↓ ↓H␈↓for␈α⊂expressions␈α∂with␈α⊂sums␈α∂and␈α⊂products.␈α∂ Assume␈α⊂that␈α∂the␈α⊂values␈α∂of␈α⊂variables␈α∂are␈α⊂given␈α⊂in␈α∂an
␈↓ ↓H␈↓␈↓αassociation list␈↓, having the form
␈↓ ↓H␈↓(<variable1>.<value1>) ... (<variablen>.<valuen>)),
␈↓ ↓H␈↓e.g. ␈↓∧((X . 5) (Y . 9.3) (Z . 2.1))␈↓.
␈↓ ↓H␈↓ The function is
␈↓ ↓H␈↓ ␈↓αnumval[e,a]␈α
←␈α
␈↓βif␈α
␈↓αnumberp␈α
e␈α
␈↓βthen␈α␈↓αe␈α
␈↓βelse␈α
if␈α
at␈α
␈↓αe␈α
␈↓βthen␈α
d␈α␈↓αassoc[e,a]␈α
␈↓βelse␈α
if␈α
a␈α
␈↓αe␈α
␈↓βeq␈α
␈↓∧PLUS␈α␈↓βthen
␈↓ ↓H␈↓β␈↓αevplus[␈↓βd ␈↓αe,a] ␈↓βelse if a ␈↓αe ␈↓βeq ␈↓∧TIMES ␈↓βthen ␈↓αevtimes[␈↓βd ␈↓αe,a]␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓ ␈↓αevplus[u,a] ← ␈↓βif n ␈↓αu ␈↓β then ␈↓∧0 ␈↓βelse ␈↓αnumval[␈↓βa ␈↓αu,a] + evplus[␈↓βd ␈↓αu,a]␈↓,
␈↓ ↓H␈↓ ␈↓αevtimes[u,a] ← ␈↓βif n ␈↓αu ␈↓β then ␈↓∧1 ␈↓βelse ␈↓αnumval[␈↓βa ␈↓αu,a] + evtimes[␈↓βd ␈↓αu,a]␈↓,
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓αassoc[x,a] ← ␈↓βif n ␈↓αa ␈↓β then ␈↓∧NIL ␈↓βelse if aa ␈↓αa ␈↓βeq ␈↓αx ␈↓βthen da ␈↓αa ␈↓βelse ␈↓αassoc[x,␈↓βd ␈↓αa]␈↓.
␈↓ ↓H␈↓␈↓ εP␈↓ :25
␈↓ ↓H␈↓β␈↓ ¬rCHAPTER II
␈↓ ↓H␈↓β␈↓ β.HOW TO WRITE RECURSIVE FUNCTION DEFINITIONS
␈↓ ↓H␈↓1. ␈↓βStatic and dynamic ways of programming.␈↓
␈↓ ↓H␈↓ In␈α∪ order␈α∪ to␈α∀ write␈α∪recursive␈α∪function␈α∪definitions,␈α∀one␈α∪must␈α∪think␈α∀about␈α∪programming
␈↓ ↓H␈↓differently␈α
than␈αis␈α
customary␈α when␈α
writing␈α
programs␈α in␈α
languages␈αlike␈α
Fortran␈αor␈α
Algol␈α
or␈αin
␈↓ ↓H␈↓machine␈α∀language.␈α∀ In␈α∀these␈α∃languages,␈α∀one␈α∀has␈α∀in␈α∀mind␈α∃the␈α∀state␈α∀of␈α∀the␈α∃ computation␈α∀ as
␈↓ ↓H␈↓represented␈α
by␈α∞ the␈α
values␈α∞of␈α
certain␈α∞variables␈α
or␈α∞locations␈α
in␈α∞the␈α
memory␈α∞of␈α
the␈α∞machine,␈α
and
␈↓ ↓H␈↓then␈α∂ one␈α∂ writes␈α∂ statements␈α∂ or␈α∂ machine␈α∂instructions␈α∞in␈α∂order␈α∂to␈α∂make␈α∂the␈α∂state␈α∂change␈α∂in␈α∞an
␈↓ ↓H␈↓appropriate way.
␈↓ ↓H␈↓ When␈αwriting␈α
LISP␈αrecursive␈α
functions␈αone␈α
thinks␈αdifferently.␈α
Namely,␈αone␈α
thinks␈αabout␈α
the
␈↓ ↓H␈↓value␈α⊂of␈α⊂the␈α⊃ function,␈α⊂ asks␈α⊂ for␈α⊂ what␈α⊃values␈α⊂ of␈α⊂the␈α⊂arguments␈α⊃the␈α⊂value␈α⊂of␈α⊂the␈α⊃function␈α⊂is
␈↓ ↓H␈↓immediate,␈α∂and,␈α∂given␈α∂ an␈α∂ arbitrary␈α∂ values␈α∂ of␈α∂ the␈α∂ arguments,␈α∂ for␈α∂ what␈α∂ simpler␈α∂arguments
␈↓ ↓H␈↓must␈α
the␈α function␈α
be␈αknown␈α
in␈αorder␈α
to␈αgive␈α
the␈αvalue␈α
of␈αthe␈α
function␈αfor␈α
the␈α given␈α
arguments.
␈↓ ↓H␈↓Let␈α us␈α take␈α a␈α numerical␈αexample;␈α namely,␈α suppose␈α we␈αwant␈αto␈αcompute␈αthe␈αfunction␈α ␈↓αn␈↓↓!␈↓.␈α For
␈↓ ↓H␈↓what␈αargument␈αis␈αthe␈αvalue␈αof␈αthe␈αfunction␈αimmediate.␈α Clearly,␈α for␈α␈↓αn␈α␈↓↓=␈α␈↓∧0␈α or␈α ␈↓αn␈α␈↓↓=␈α␈↓∧1␈↓,␈αthe␈αvalue␈αis
␈↓ ↓H␈↓immediately␈α
seen␈α∞to␈α
be␈α
1.␈α∞ Moreover,␈α
we␈α
can␈α∞get␈α
the␈α
value␈α∞for␈α
an␈α
arbitrary␈α∞ ␈↓αn␈↓␈α
if␈α
we␈α∞know␈α
the
␈↓ ↓H␈↓value␈α∞ for␈α∞␈↓αn␈↓↓-␈↓∧1.␈α∞ Also,␈α∂we␈α∞see␈α∞that␈α∞knowing␈α∞the␈α∂value␈α∞for␈α∞ ␈↓αn␈α∞␈↓↓=␈α∞␈↓∧1␈α∂ is␈α∞redundant,␈α∞since␈α∞it␈α∂can␈α∞be
␈↓ ↓H␈↓∧obtained␈αfrom␈αthe␈α ␈↓αn␈α
␈↓↓=␈α␈↓∧0␈α case␈αby␈αthe␈α
same␈α rule␈α as␈αgets␈α it␈α
for␈α a␈α general␈α ␈↓αn␈↓∧␈α from␈α
the␈αvalue
␈↓ ↓H␈↓∧for ␈↓αn␈↓↓-␈↓∧1␈↓. All this talk leads to the simple recursive formula:
␈↓ ↓H␈↓ ␈↓αn␈↓↓! ← ␈↓βif ␈↓αn ␈↓↓= ␈↓∧0 ␈↓βthen ␈↓∧1 ␈↓βelse ␈↓αn␈↓↓(␈↓αn␈↓↓-␈↓∧1␈↓↓)!.
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓We␈αmay␈αregard␈αthis␈αas␈αa␈αstatic␈αway␈αof␈αlooking␈αat␈αprogramming.␈α We␈αask␈αwhat␈αsimpler␈αcases
␈↓ ↓H␈↓the␈α
general␈αcase␈α
of␈α
our␈αfunction␈α
depends␈α
on␈αrather␈α
than␈α
how␈α we␈α
build␈α
up␈αthe␈α
desired␈α
state␈αof
␈↓ ↓H␈↓the␈αcomputation.␈α
One␈αoften␈α
is␈αled␈α
to␈αbelieve␈αthat␈α
static␈α=␈α
bad␈α and␈α
dynamic␈α=␈α
good,␈αbut␈α in␈α
this
␈↓ ↓H␈↓case,␈α the␈αstatic␈αway␈αis␈αoften␈αbetter␈αthan␈αthe␈αdynamic␈αway.␈α Perhaps␈αthis␈αdistinction␈αis␈αequivalent␈αto
␈↓ ↓H␈↓what␈α∞some␈α∞people␈α∞call␈α
the␈α∞distinction␈α∞between␈α∞␈↓αtop-down␈↓␈α
and␈α∞␈↓αbottom-up␈↓␈α∞programming␈α∞with␈α
␈↓αstatic␈↓
␈↓ ↓H␈↓corresponding␈αto␈α␈↓αtop-down␈↓.␈α LISP␈αoffers␈αboth,␈αbut␈αthe␈αstatic␈αstyle␈αis␈αbetter␈αdeveloped␈αin␈αLISP,␈αand
␈↓ ↓H␈↓we will emphasize it.
␈↓ ↓H␈↓␈↓ ¬rCHAPTER II␈↓ :26
␈↓ ↓H␈↓ Compare␈α∂ the␈α∂ above␈α∂ recursive␈α∂ definition␈α∂with␈α∂the␈α∂following␈α∂obvious␈α∂Algol␈α∂program␈α∞for
␈↓ ↓H␈↓computing ␈↓αn␈↓↓!:
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βinteger procedure ␈↓αfactorial␈↓↓(␈↓αn␈↓↓); ␈↓βinteger ␈↓αs␈↓↓;
␈↓ ↓H␈↓↓␈↓βbegin
␈↓ ↓H␈↓β␈↓ ␈↓β␈↓αs ␈↓↓:= ␈↓∧1␈↓↓;
␈↓ ↓H␈↓↓␈↓αloop␈↓↓: ␈↓βif ␈↓αn ␈↓↓= ␈↓∧0 ␈↓βthen go to ␈↓αdone␈↓↓;
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓αs ␈↓↓:= ␈↓αn␈↓↓*␈↓αs␈↓↓;
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓αn ␈↓↓:= ␈↓αn␈↓↓-␈↓∧1␈↓↓;
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βgo to ␈↓αloop␈↓↓;
␈↓ ↓H␈↓↓␈↓αdone␈↓↓: ␈↓αfactorial ␈↓↓:= ␈↓αs␈↓↓;
␈↓ ↓H␈↓↓␈↓βend␈↓↓;
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓The␈α⊂ LISP␈α∂program␈α⊂is␈α∂shorter␈α⊂and␈α∂clearer␈α⊂in␈α∂this␈α⊂particularly␈α∂favorable␈α⊂ case.␈α∂ Actually,
␈↓ ↓H␈↓when␈α we␈α discuss␈α
the␈α mechanism␈α of␈α
recursion,␈αit␈αwill␈α
turn␈αout␈αthat␈α
the␈αLISP␈αprogram␈α
will␈αbe
␈↓ ↓H␈↓inefficient␈α∪in␈α∩using␈α∪the␈α∩pushdown␈α∪mechanism␈α∪unnecessarily␈α∩and␈α∪should␈α∩be␈α∪ replaced␈α∪by␈α∩ the
␈↓ ↓H␈↓following␈α∃ somewhat␈α∃ longer␈α∀ program␈α∃that␈α∃corresponds␈α∃to␈α∀the␈α∃above␈α∃Algol␈α∃program␈α∀rather
␈↓ ↓H␈↓precisely:
␈↓ ↓H␈↓ ␈↓αn␈↓↓! ← ␈↓αfact␈↓↓(␈↓αn␈↓↓, ␈↓αs␈↓↓),
␈↓ ↓H␈↓↓␈↓where
␈↓ ↓H␈↓ ␈↓αfact␈↓↓(␈↓αn␈↓↓, ␈↓αs␈↓↓) ← ␈↓βif ␈↓αn ␈↓↓= ␈↓∧0 ␈↓βthen ␈↓αs ␈↓βelse ␈↓αfact␈↓↓(␈↓αn␈↓↓-␈↓∧1␈↓↓, ␈↓αn␈↓↓*␈↓αs␈↓↓).
␈↓ ↓H␈↓↓␈↓In fact, compilers should produce the same object code from the two programs.
␈↓ ↓H␈↓2. ␈↓βSimple list recursion.␈↓
␈↓ ↓H␈↓ About␈α
the␈α
simplest␈α
form␈α
of␈α
recursion␈α
in␈α
LISP␈αoccurs␈α
when␈α
one␈α
of␈α
the␈α
arguments␈α
is␈α
a␈αlist,␈α
the
␈↓ ↓H␈↓result␈αis␈αimmediate␈αwhen␈αthe␈αargument␈αis␈αnull,␈αand␈αotherwise␈αwe␈αneed␈αonly␈αknow␈αthe␈αresult␈αfor␈αthe
␈↓ ↓H␈↓␈↓βd␈↓-part␈α∂of␈α∂that␈α∂argument.␈α∂ Consider,␈α∂for␈α∂example,␈α∞ ␈↓αu␈↓↓*␈↓αv␈↓,␈α∂the␈α∂concatenation␈α∂of␈α∂the␈α∂lists␈α∂ ␈↓αu␈↓␈α∂ and␈α∞␈↓αv␈↓.
␈↓ ↓H␈↓The␈αresult␈αis␈αimmediate␈αfor␈α the␈α case␈α ␈↓βn␈α␈↓αu␈↓␈α and␈αotherwise␈αdepends␈αon␈αthe␈αresult␈αfor␈α ␈↓βd␈α␈↓αu␈↓.␈α Thus,
␈↓ ↓H␈↓we have
␈↓ ↓H␈↓ ␈↓αu␈↓↓*␈↓αv ␈↓↓← ␈↓βif n ␈↓αu ␈↓βthen ␈↓αv ␈↓βelse a ␈↓αu ␈↓↓. [␈↓βd ␈↓αu ␈↓↓* ␈↓αv␈↓↓].
␈↓ ↓H␈↓↓␈↓On␈α∞the␈α∞other␈α∞hand,␈α∞if␈α∞we␈α∞had␈α∞tried␈α∞to␈α∞recur␈α∞on␈α∞ ␈↓αv␈↓␈α∞ rather␈α∞than␈α∞on␈α∞ ␈↓αu␈↓␈α∞we␈α∞would␈α∞not␈α∞have␈α∞been
␈↓ ↓H␈↓successful.␈α The␈α
result␈αwould␈α
be␈αimmediate␈α
for␈α␈↓βn␈α␈↓αv␈↓,␈α
but␈α ␈↓αu␈↓↓*␈↓αv␈↓␈α
cannot␈αbe␈α
constructed␈αin␈α
any␈αdirect
␈↓ ↓H␈↓way␈α∞from␈α∞ ␈↓αu␈↓↓*␈↓βd␈α∞␈↓αv␈↓␈α∞without␈α∞ a␈α∞ function␈α∞ that␈α∞ puts␈α∞ an␈α∞ element␈α∞onto␈α∞the␈α∞end␈α∞of␈α∞a␈α∞list.␈α∂ (From␈α∞a
␈↓ ↓H␈↓strictly␈α∂list␈α∂point␈α∂of␈α∂view,␈α∂such␈α∂ a␈α⊂ function␈α∂ would␈α∂ be␈α∂ as␈α∂elementary␈α∂ as␈α∂ ␈↓αcons␈↓␈α∂ which␈α⊂puts␈α∂an
␈↓ ↓H␈↓element␈αonto␈αthe␈αfront␈αof␈αa␈αlist,␈αbut,␈αwhen␈αwe␈αconsider␈αthe␈αimplementation␈αof␈αlists␈αby␈αlist␈αstructures,
␈↓ ↓H␈↓we␈α see␈α that␈α the␈α function␈αis␈αnot␈αso␈αelementary.␈α This␈αhas␈αled␈αsome␈αpeople␈αto␈αconstruct␈αsystems␈αin
␈↓ ↓H␈↓␈↓ ¬rCHAPTER II␈↓ :27
␈↓ ↓H␈↓which␈αlists␈αare␈α bi-directional,␈α but,␈αin␈α the␈α main,␈α this␈αhas␈αturned␈αout␈αto␈αbe␈αa␈αbad␈αidea).␈α Anyway,
␈↓ ↓H␈↓it is usually easier to recur on one argument of a function than to recur on the other.
␈↓ ↓H␈↓ It␈α∞ is␈α
often␈α∞necessary␈α∞to␈α
represent␈α∞a␈α
correspondence␈α∞between␈α∞the␈α
elements␈α∞of␈α
a␈α∞small␈α∞set␈α
of
␈↓ ↓H␈↓atoms␈α
and␈α
certain␈α
S-expressions␈α
by␈α
a␈α
list␈α
structure.␈α
This␈α
is␈α
conveniently␈α
done␈α
by␈α
means␈α
of␈α
an
␈↓ ↓H␈↓association␈αlist␈αwhich␈αis␈αa␈αlist␈αof␈αpairs,␈αeach␈αpair␈αconsisting␈αof␈α an␈α atom␈α and␈αthe␈αcorresponding␈α
S-
␈↓ ↓H␈↓expression. Thus the association list
␈↓ ↓H␈↓ ␈↓∧((X . (PLUS A B)) (Y . C) (Z . (TIMES U V)),
␈↓ ↓H␈↓∧␈↓which would print as
␈↓ ↓H␈↓ ␈↓∧((X PLUS A B)) (Y . C) (Z TIMES U V)),
␈↓ ↓H␈↓∧␈↓pairs␈α
␈↓∧X␈↓␈α∞ with␈α
␈↓∧(PLUS␈α
A␈α∞B),␈α
Y␈↓␈α∞ with␈α
␈↓∧C␈↓,␈α
etc.␈α∞We␈α
need␈α∞a␈α
function␈α
to␈α∞tell␈α
whether␈α∞ anything␈α
is
␈↓ ↓H␈↓associated with the atom ␈↓αx␈↓ in the association list ␈↓αa␈↓, and, if so, to tell us what. We have
␈↓ ↓H␈↓ ␈↓αassoc␈↓↓[␈↓αx, a␈↓↓] ← ␈↓βif n ␈↓αa ␈↓βthen ␈↓∧NIL ␈↓βelse if ␈↓αx ␈↓βeq aa ␈↓αa ␈↓βthen a ␈↓αa
␈↓ ↓H␈↓α␈↓ ␈↓α␈↓βelse ␈↓αassoc␈↓↓[␈↓αx, ␈↓βd ␈↓αa␈↓↓].
␈↓ ↓H␈↓↓␈↓Its␈α
value␈α
is␈α
␈↓∧NIL␈↓␈α
if␈α
nothing␈α
is␈α
associated␈α
with␈α
␈↓αx␈↓␈α
and␈α
the␈α
association␈α
pair␈α∞otherwise.␈α
E.g.
␈↓ ↓H␈↓␈↓αassoc␈↓↓[␈↓∧X, ((X.W) (Y.V))␈↓↓] = ␈↓∧(X.W)␈↓.
␈↓ ↓H␈↓ It␈α
commonly␈α
happens␈α
that␈α
a␈α
function␈α
has␈α
no␈α
simple␈α
recursion,␈α
but␈α
there␈α
is␈α
a␈αsimple␈α
recursion
␈↓ ↓H␈↓for␈α⊃a␈α∩function␈α⊃with␈α⊃one␈α∩more␈α⊃variable␈α⊃that␈α∩ reduces␈α⊃ to␈α⊃the␈α∩desired␈α⊃function␈α⊃when␈α∩the␈α⊃extra
␈↓ ↓H␈↓variable is set to ␈↓∧NIL␈↓. Thus
␈↓ ↓H␈↓ ␈↓αreverse␈↓↓[␈↓αu␈↓↓] ← ␈↓αrev1␈↓↓[␈↓αx, ␈↓∧NIL␈↓↓],
␈↓ ↓H␈↓↓␈↓where
␈↓ ↓H␈↓ ␈↓αrev1␈↓↓[␈↓αu, v␈↓↓] ← ␈↓βif n ␈↓αu ␈↓βthen ␈↓αv ␈↓βelse ␈↓αrev1␈↓↓[␈↓βd ␈↓αu, ␈↓βa ␈↓αu . v␈↓↓].
␈↓ ↓H␈↓↓␈↓αreverse␈↓␈α
has␈αa␈α
direct␈αrecursive␈α
definition␈α
as␈αdiscovered␈α
by␈αS.␈α
Ness,␈α
but␈αno-one␈α
would␈αwant␈α
to␈αuse␈α
the
␈↓ ↓H␈↓following␈α∞in␈α∞actual␈α∞computation␈α∞ nor␈α∞does␈α∞ it␈α∞generate␈α∞much␈α∞understanding,␈α∞only␈α∞appreciation␈α∞of
␈↓ ↓H␈↓Mr. Ness's ingenuity:
␈↓ ↓H␈↓ ␈↓αreverse␈↓↓[␈↓αu␈↓↓] ← ␈↓βif n ␈↓αu ␈↓↓∨ ␈↓βn d ␈↓αu ␈↓βthen ␈↓αu ␈↓βelse
␈↓ ↓H␈↓β␈↓ ␈↓βa ␈↓αreverse␈↓↓[␈↓βd ␈↓αu␈↓↓] . ␈↓αreverse␈↓↓[␈↓βa ␈↓αu. reverse␈↓↓[␈↓βd ␈↓αreverse␈↓↓[␈↓βd ␈↓αu␈↓↓]]].
␈↓ ↓H␈↓ Exercises
␈↓ ↓H␈↓ 1.␈αUsing␈α
the␈αfunction␈α ␈↓αmember␈↓↓[␈↓αx,␈α
u␈↓↓]␈↓␈α defined␈αin␈α
Chapter␈αI␈αwhich␈α
may␈αalso␈αbe␈α
written␈α ␈↓αx␈α␈↓↓ε␈α
␈↓αu␈↓,
␈↓ ↓H␈↓write␈αfunction␈αdefinitions␈αfor␈αthe␈αunion␈α ␈↓αu␈α␈↓↓∪␈α␈↓αv␈↓␈α of␈αlists␈α ␈↓αu␈↓␈α and␈α ␈↓αv␈↓,␈αthe␈αintersection␈α ␈↓αu␈α␈↓↓∩␈α␈↓αv␈↓,␈α and␈α the
␈↓ ↓H␈↓set difference ␈↓αu␈↓↓-␈↓αv␈↓. What is wanted should be clear from the examples:
␈↓ ↓H␈↓␈↓ ¬rCHAPTER II␈↓ :28
␈↓ ↓H␈↓ ␈↓∧(A B C) ␈↓↓∪␈↓∧ (B C D) ␈↓↓=␈↓∧ (A B C D),
␈↓ ↓H␈↓∧␈↓ ␈↓∧(A B C) ␈↓↓∩␈↓∧ (B C D) ␈↓↓=␈↓∧ (B C),
␈↓ ↓H␈↓∧␈↓and
␈↓ ↓H␈↓ ␈↓∧(A B C) ␈↓↓-␈↓∧ (B C D) ␈↓↓=␈↓∧ (A).
␈↓ ↓H␈↓∧␈↓Pay␈α∂ attention␈α∂ to␈α∂getting␈α∂correct␈α∂the␈α∂trivial␈α∂cases␈α∂in␈α∂which␈α∂some␈α∂of␈α∂the␈α∂arguments␈α∂are␈α⊂␈↓∧NIL␈↓.␈α∂ In
␈↓ ↓H␈↓general, it is important to understand clearly the trivial cases of functions.
␈↓ ↓H␈↓ 2.␈α⊂ Suppose␈α⊂ ␈↓αx␈↓␈α⊂ takes␈α⊃ numbers␈α⊂ as␈α⊂ values␈α⊂and␈α⊂ ␈↓αu␈↓␈α⊃ takes␈α⊂as␈α⊂values␈α⊂lists␈α⊂of␈α⊃numbers␈α⊂in
␈↓ ↓H␈↓ascending␈α∞order,␈α∞e.g.␈α∞ ␈↓∧(2␈α∞4␈α∞7)␈↓.␈α∞ Write␈α
a␈α∞function␈α∞ ␈↓αmerge␈↓↓[␈↓αx,␈α∞u␈↓↓]␈↓␈α∞ whose␈α∞ value␈α∞ is␈α∞obtained␈α
from
␈↓ ↓H␈↓that␈αof␈α ␈↓αu␈↓␈α by␈αputting␈α ␈↓αx␈↓␈α in␈α ␈↓αu␈↓␈α in␈α its␈α proper␈α place.␈α Thus␈α␈↓αmerge␈↓↓[␈↓∧3,␈α(2␈α4)␈↓↓]␈α=␈α␈↓∧(2␈α3
␈↓ ↓H␈↓∧4)␈↓, and ␈↓αmerge␈↓↓[␈↓∧3, (2 3)␈↓↓] = ␈↓∧(2 3 3)␈↓.
␈↓ ↓H␈↓ 3.␈α∂ Write␈α∞ functions␈α∂ giving␈α∞the␈α∂union,␈α∞intersection,␈α∂and␈α∞set␈α∂difference␈α∞of␈α∂ordered␈α∂lists;␈α∞the
␈↓ ↓H␈↓result is wanted as an ordered list.
␈↓ ↓H␈↓ Note␈α⊂that␈α∂computing␈α⊂these␈α∂functions␈α⊂of␈α⊂unordered␈α∂lists␈α⊂ takes␈α∂a␈α⊂ number␈α⊂ of␈α∂comparisons
␈↓ ↓H␈↓proportional␈αto␈αthe␈αsquare␈α
of␈αthe␈αnumber␈αof␈α
elements␈αof␈αa␈αtypical␈α
list,␈αwhile␈αfor␈αordered␈α
lists,␈α the
␈↓ ↓H␈↓number of comparisons is proportional to the number of elements.
␈↓ ↓H␈↓ 4.␈α
Using␈α
␈↓αmerge␈↓,␈α∞write␈α
a␈α
function␈α∞ ␈↓αsort␈↓␈α
that␈α
transforms␈α
an␈α∞unordered␈α
list␈α
into␈α∞an␈α
ordered
␈↓ ↓H␈↓list.
␈↓ ↓H␈↓ 5.␈α∃Write␈α⊗a␈α∃function␈α∃ ␈↓αgoodsort␈↓␈α⊗ that␈α∃ sorts␈α⊗ a␈α∃ list␈α∃ using␈α⊗ a␈α∃number␈α⊗ of␈α∃ comparisons
␈↓ ↓H␈↓proportional to ␈↓αn log n␈↓, where ␈↓αn␈↓ is the length of the list to be sorted.
␈↓ ↓H␈↓3. ␈↓βSimple S-expression recursion.␈↓
␈↓ ↓H␈↓ In␈αanother␈αclass␈αof␈αproblems,␈αthe␈αvalue␈αof␈α the␈α function␈α is␈αimmediate␈α for␈α
atomic␈αsymbols,
␈↓ ↓H␈↓and␈α∞for␈α∞non␈α∞atoms␈α∞depends␈α∞only␈α∞on␈α∞the␈α∞values␈α∞for␈α∞ the␈α∞a-part␈α∞and␈α∞the␈α∞d-part␈α∞of␈α∞the␈α
argument.
␈↓ ↓H␈↓Thus ␈↓αsubst␈↓ was defined by
␈↓ ↓H␈↓ ␈↓αsubst␈↓↓[␈↓αx, y, z␈↓↓] ← ␈↓βif at ␈↓αz ␈↓βthen ␈↓↓[␈↓βif ␈↓αz ␈↓βeq ␈↓αy ␈↓βthen ␈↓αx ␈↓βelse ␈↓αz␈↓↓]
␈↓ ↓H␈↓↓ ␈↓βelse ␈↓αsubst␈↓↓[␈↓αx, y, ␈↓βa ␈↓αz␈↓↓] . ␈↓αsubst␈↓↓[␈↓αx , y , ␈↓βd ␈↓αz␈↓↓].
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓Two␈α⊃other␈α⊃examples␈α⊃are␈α⊃ ␈↓αequal␈↓␈α⊃ which␈α⊃gives␈α⊃ the␈α⊃ equality␈α⊃ of␈α⊃S-expressions␈α⊃ and␈α⊂ ␈↓αflat␈↓
␈↓ ↓H␈↓which spreads an S-expression into a list of atoms: They are defined by
␈↓ ↓H␈↓ ␈↓αx␈↓↓=␈↓αy ␈↓↓← ␈↓αx ␈↓βeq ␈↓αy ␈↓↓∨ [¬␈↓βat ␈↓αx ␈↓↓∧ ¬␈↓βat ␈↓αy ␈↓↓∧ ␈↓βa ␈↓αx ␈↓↓= ␈↓βa ␈↓αy ␈↓↓∧ ␈↓βd ␈↓αx ␈↓↓= ␈↓βd ␈↓αy␈↓↓],
␈↓ ↓H␈↓↓␈↓and
␈↓ ↓H␈↓␈↓ ¬rCHAPTER II␈↓ :29
␈↓ ↓H␈↓ ␈↓αflat␈↓↓[␈↓αx␈↓↓] ← ␈↓αflata␈↓↓[␈↓αx, ␈↓∧NIL␈↓↓]
␈↓ ↓H␈↓↓␈↓where
␈↓ ↓H␈↓ ␈↓αflata[x,u] ← ␈↓βif at ␈↓αx ␈↓βthen ␈↓αx.y ␈↓βelse ␈↓αflata[␈↓βa ␈↓αx,flata[␈↓βd ␈↓αx,y]]␈↓.
␈↓ ↓H␈↓ EXERCISES
␈↓ ↓H␈↓ 1.␈α
Write␈α
a␈αpredicate␈α
to␈α
tell␈α
whether␈αa␈α
given␈α
atom␈α
occurs␈αin␈α
a␈α
given␈α
S-expression,␈αe.g.␈α
␈↓αoccur[␈↓∧B,
␈↓ ↓H␈↓∧((A.B).C)␈↓↓] = ␈↓∧T␈↓.
␈↓ ↓H␈↓ 2. Write a predicate to tell how many times a given atom occurs in an S-expression.
␈↓ ↓H␈↓ 3.␈α∞ Write␈α∞ a␈α∞ function␈α∞to␈α
make␈α∞a␈α∞list␈α∞without␈α∞duplications␈α
of␈α∞the␈α∞atoms␈α∞occurring␈α∞in␈α∞an␈α
S-
␈↓ ↓H␈↓expression.
␈↓ ↓H␈↓ 4.␈αWrite␈αa␈α
function␈αto␈αmake␈αa␈α
list␈αof␈αall␈α
atoms␈α that␈α occur␈αmore␈α
than␈α once␈α in␈α a␈α
given
␈↓ ↓H␈↓S-expression paired with their multiplicities.
␈↓ ↓H␈↓ 5.␈α
Write␈αa␈α
predicate␈α
to␈αtell␈α
whether␈αan␈α
S-expression␈α
has␈αmore␈α
than␈α
one␈αoccurrence␈α
of␈αa␈α
given
␈↓ ↓H␈↓S-expression as a sub-expression.
␈↓ ↓H␈↓4. ␈↓βOther structural recursions.␈↓
␈↓ ↓H␈↓ When␈α↔lists␈α_are␈α↔used␈α_to␈α↔represent␈α_algebraic␈α↔expressions,␈α_functions␈α↔of␈α_these␈α↔algebraic
␈↓ ↓H␈↓expressions␈α⊗often␈α⊗have␈α⊗a␈α⊗recursive␈α⊗form␈α⊗closely␈α⊗related␈α⊗to␈α⊗the␈α⊗inductive␈α⊗definition␈α⊗of␈α∃the
␈↓ ↓H␈↓expressions.␈α⊂ Suppose,␈α⊂for␈α⊂example,␈α⊂that␈α⊂sums␈α⊂and␈α⊂products␈α⊂are␈α⊂represented␈α⊂respectively␈α⊂by␈α∂the
␈↓ ↓H␈↓forms␈α
␈↓∧(PLUS␈α
X␈αY)␈↓␈α
and␈α
␈↓∧(TIMES␈α
X␈αY)␈↓␈α
and␈α
that␈α
the␈αvalues␈α
of␈α
variables␈α
are␈αgiven␈α
on␈α
an␈αa-list.␈α
We
␈↓ ↓H␈↓can write a recursive formula for the value of an expression as follows:
␈↓ ↓H␈↓ ␈↓αvalue␈↓↓[␈↓αe, a␈↓↓] ← ␈↓βif at ␈↓αe ␈↓βthen d ␈↓αassoc␈↓↓[␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe ␈↓βeq ␈↓∧PLUS ␈↓βthen ␈↓αvalue␈↓↓[␈↓βad ␈↓αe, a␈↓↓] + ␈↓αvalue␈↓↓[␈↓βadd ␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe ␈↓βeq ␈↓∧TIMES ␈↓βthen ␈↓αvalue␈↓↓[␈↓βad ␈↓αe, a␈↓↓] * ␈↓αvalue␈↓↓[␈↓βadd ␈↓αe, a␈↓↓].
␈↓ ↓H␈↓↓␈↓On␈αthe␈αother␈αhand,␈αsuppose␈αthat␈αsums␈αand␈αproducts␈αare␈αnot␈αrestricted␈αto␈αhave␈αjust␈αtwo␈αarguments;
␈↓ ↓H␈↓then we must use auxiliary functions to define the value of an expression, as follows:
␈↓ ↓H␈↓ ␈↓αvalue␈↓↓[␈↓αe, a␈↓↓] ← ␈↓βif at ␈↓αe ␈↓βthen d ␈↓αassoc␈↓↓[␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓ ␈↓βelse if a ␈↓αe ␈↓βeq ␈↓∧PLUS ␈↓βthen ␈↓αvplus␈↓↓[␈↓βd ␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓ ␈↓βelse if a ␈↓αe ␈↓βeq ␈↓∧TIMES ␈↓βthen ␈↓αvtimes␈↓↓[␈↓βd ␈↓αe, a␈↓↓].
␈↓ ↓H␈↓↓␈↓where
␈↓ ↓H␈↓ ␈↓αvplus␈↓↓[␈↓αu, a␈↓↓] ← ␈↓βif n ␈↓αu ␈↓βthen ␈↓∧0 ␈↓βelse ␈↓αvalue␈↓↓[␈↓βa ␈↓αu, a␈↓↓] + ␈↓αvplus␈↓↓[␈↓βd ␈↓αu, a␈↓↓],
␈↓ ↓H␈↓␈↓ ¬rCHAPTER II␈↓ :30
␈↓ ↓H␈↓↓␈↓and
␈↓ ↓H␈↓ ␈↓αvtimes␈↓↓[␈↓αu, a␈↓↓] ← ␈↓βif n ␈↓αu ␈↓βthen ␈↓∧1 ␈↓βelse ␈↓αvalue␈↓↓[␈↓βa ␈↓αu, a␈↓↓] * ␈↓αvtimes␈↓↓[␈↓βd ␈↓αu, a␈↓↓].
␈↓ ↓H␈↓↓␈↓In␈α
both␈α
cases,␈αthe␈α
recursion␈α
form␈αis␈α
related␈α
to␈αthe␈α
structure␈α
of␈αthe␈α
algebraic␈α
expressions␈αmore␈α
closely
␈↓ ↓H␈↓than to the structure of S-expressions or lists.
␈↓ ↓H␈↓5. ␈↓βTree search recursion.␈↓
␈↓ ↓H␈↓ We␈α
begin␈α
with␈α
a␈α
general␈α
depth␈α
first␈α
tree␈α
search␈α
function.␈α
It␈α
can␈α
be␈α
used␈α
to␈α
search␈α
specific
␈↓ ↓H␈↓trees␈αof␈α
possibilities␈αby␈α
defining␈αthree␈α
auxiliary␈αfunctions␈α
in␈αa␈α
way␈αthat␈α
depends␈αon␈αthe␈α
application.
␈↓ ↓H␈↓We have
␈↓ ↓H␈↓ ␈↓αsearch p ␈↓↓← ␈↓βif ␈↓αlose p ␈↓βthen ␈↓LOSE
␈↓ ↓H␈↓ ␈↓βelse if ␈↓αter p ␈↓βthen ␈↓αp ␈↓βelse ␈↓αsearchlis␈↓↓[␈↓αsuccessors p␈↓↓]␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓ ␈↓αsearchlis␈αu␈α␈↓↓←␈α␈↓β␈αif␈αn␈α␈↓αu␈α␈↓βthen␈α␈↓LOSE␈α␈↓βelse␈α␈↓α␈↓↓{␈↓αsearch␈α␈↓βa␈α ␈↓αu␈↓↓}[␈↓αλx␈↓↓:␈↓α␈α␈↓βif␈α␈↓αx␈α␈↓↓=␈α␈↓LOSE␈α␈↓βthen␈α␈↓αsearchlis␈α␈↓βd␈α␈↓αu␈α␈↓βelse
␈↓ ↓H␈↓β␈↓αx␈↓↓]␈↓α.␈↓
␈↓ ↓H␈↓In␈α
the␈α
applications,␈α
we␈α
start␈α
with␈α
a␈α
position␈α
␈↓αp␈↓¬0␈↓,␈α and␈α
we␈α
are␈α
looking␈α
for␈α
a␈α
win␈α
in␈α
the␈αsuccessor␈α
tree
␈↓ ↓H␈↓of␈α␈↓αp␈↓¬0␈↓.␈α Certain␈αpositions␈αlose␈αand␈α there␈αis␈α no␈αpoint␈αlooking␈α at␈αtheir␈α successors.␈α This␈α
is␈αdecided
␈↓ ↓H␈↓by␈α
the␈α
predicate␈α
␈↓αlose␈↓.␈α
A␈α
position␈α
is␈α
a␈α
win␈α
if␈α
it␈α
doesn't␈α
lose␈α
and␈α
it␈α
satisfies␈α
the␈α
predicate␈α␈↓αter␈↓.
␈↓ ↓H␈↓The␈αsuccessors␈αof␈αa␈αposition␈α is␈αgiven␈αby␈αthe␈α function␈α␈↓αsuccessors␈↓,␈αand␈α the␈α value␈α of␈α␈↓αsearch␈α
p␈↓␈α is
␈↓ ↓H␈↓the␈α∂ winning␈α∂position.␈α⊂ No␈α∂non-losing␈α∂position␈α⊂should␈α∂have␈α∂the␈α⊂ name␈α∂LOSE␈α∂or␈α⊂the␈α∂function
␈↓ ↓H␈↓won't work properly.
␈↓ ↓H␈↓ Simple␈α
S-expression␈α
recursion␈α
can␈αbe␈α
regarded␈α
as␈α
a␈α
special␈αcase␈α
of␈α
depth␈α
first␈α
recursion.␈α It␈α
is
␈↓ ↓H␈↓special␈αin␈αthat␈αthere␈αexactly␈αtwo␈αbranches,␈αbut␈αeven␈αmore␈αimportant,␈αthe␈αtree␈αis␈αthe␈αtree␈αof␈αparts␈αof
␈↓ ↓H␈↓the␈α
S-expression␈α
and␈α
is␈α
present␈α
at␈α
the␈α
beginning␈α
of␈α
the␈α
calculation.␈α
In␈α
case␈α
of␈α
tree␈α
search␈α
recursion,
␈↓ ↓H␈↓the tree is generated by the ␈↓αsuccessors␈↓ function.
␈↓ ↓H␈↓ Our␈α∞first␈α∞application␈α∞is␈α∞ finding␈α∞a␈α∞path␈α∞from␈α
an␈α∞ initial␈α∞node␈α∞to␈α∞a␈α∞final␈α∞node␈α∞ in␈α∞a␈α
graph
␈↓ ↓H␈↓represented␈α
by␈α
a␈α
list␈α
structure␈α
as␈α
described␈α
in␈αchapter␈α
I.␈α
A␈α
position␈α
is␈α
a␈α
path␈α
starting␈α
from␈αthe
␈↓ ↓H␈↓initial␈α
node␈αand␈α
continuing␈α
to␈α some␈α
intermediate␈α node␈α
and␈α
is␈αrepresented␈α
by␈α
a␈αlist␈α
of␈αits␈α
nodes
␈↓ ↓H␈↓in reverse order. The three functions for this application are
␈↓ ↓H␈↓ ␈↓αlose p ␈↓↓← ␈↓βa ␈↓αp ␈↓↓ε ␈↓βd ␈↓αp,
␈↓ ↓H␈↓α␈↓ ␈↓αter p ␈↓↓← [␈↓βa ␈↓αp ␈↓↓=␈↓α final␈↓↓]␈↓α, ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ¬rCHAPTER II␈↓ :31
␈↓ ↓H␈↓ ␈↓αsuccessors p ␈↓↓←␈↓α mapcar␈↓↓[␈↓βd ␈↓αassoc␈↓↓[␈↓βa ␈↓αp, graph␈↓↓]␈↓α, λx␈↓↓:␈↓α x.p␈↓↓]␈↓α.␈↓
␈↓ ↓H␈↓ Another␈α
example␈αis␈α
the␈α
so-called␈α␈↓αInstant␈α
Insanity␈↓␈αpuzzle.␈α
There␈α
are␈αfour␈α
cubical␈αblocks,␈α
and
␈↓ ↓H␈↓each␈αface␈αof␈α
each␈αblock␈αis␈α
colored␈αwith␈αone␈α
of␈αfour␈αcolors.␈α The␈α
object␈αof␈αthe␈α
puzzle␈αis␈αto␈α
build␈αa
␈↓ ↓H␈↓tower␈αof␈αall␈αfour␈αblocks␈αsuch␈αthat␈αeach␈αvertical␈αface␈αof␈αthe␈αtower␈αinvolves␈αall␈αfour␈αcolors.␈α In␈αorder
␈↓ ↓H␈↓to␈α∞use␈α∂the␈α∞above␈α∂defined␈α∞function␈α∞␈↓αsearch␈↓␈α∂for␈α∞this␈α∂purpose,␈α∞we␈α∞must␈α∂define␈α∞the␈α∂representation␈α∞of
␈↓ ↓H␈↓positions␈α
and␈α
give␈α
the␈α
functions␈α
␈↓αlose,␈α
ter,␈α
␈↓and␈α
␈↓αsuccessors␈↓.␈α
A␈α
position␈α
is␈α
represented␈α
by␈α
a␈α
list␈αof␈α
lists
␈↓ ↓H␈↓¬␈α
one␈α
for␈α
each␈α
face␈α
of␈αthe␈α
tower.␈α
Each␈α
sublist␈α
is␈α
the␈α
list␈αof␈α
colors␈α
of␈α
the␈α
faces␈α
of␈α
the␈αblocks␈α
showing
␈↓ ↓H␈↓in␈α⊃that␈α⊂face.␈α⊃ We␈α⊃shall␈α⊂assume␈α⊃that␈α⊂the␈α⊃blocks␈α⊃are␈α⊂described␈α⊃in␈α⊂the␈α⊃following␈α⊃longwinded␈α⊂but
␈↓ ↓H␈↓convenient␈αway.␈α (We'll␈αtake␈αup␈αprecomputing␈αthis␈αdescription␈αlater.)␈α For␈αeach␈αblock␈αthere␈αis␈αa␈αlist
␈↓ ↓H␈↓of␈αthe␈α
24␈αorientations␈α
of␈αthe␈α
block␈αwhere␈α
each␈αorientation␈α
is␈αdescribed␈α
as␈αa␈α
list␈αof␈α
the␈αcolors␈α
around
␈↓ ↓H␈↓the␈αvertical␈αfaces␈αof␈αthe␈αblock␈αwhen␈αit␈αis␈αin␈αthat␈αorientation.␈α Thus␈αthe␈αpuzzle␈αis␈αdescribed␈αby␈αa␈αlist
␈↓ ↓H␈↓of lists of lists which we shall call ␈↓αpuzz␈↓.
␈↓ ↓H␈↓ We now have
␈↓ ↓H␈↓ ␈↓αp␈↓¬0␈↓ ␈↓↓=␈↓ (NIL NIL NIL NIL),
␈↓ ↓H␈↓ ␈↓αlose p ␈↓↓←␈↓α orlis␈↓↓[␈↓αp, λu␈↓↓:␈↓α ␈↓βa ␈↓αu ␈↓↓ε ␈↓βd ␈↓αu␈↓↓]␈↓α,
␈↓ ↓H␈↓α␈↓ ␈↓αter p ␈↓↓← [␈↓αlength ␈↓βa ␈↓αp ␈↓↓=␈↓α 4␈↓↓]␈↓,
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓αsuccessors p ␈↓↓←␈↓α mapcar␈↓↓[␈↓βa ␈↓αnth␈↓↓[␈↓αpuzz, 1 + length ␈↓βa ␈↓αp␈↓↓]␈↓α , λx␈↓↓:␈↓α mapcar2␈↓↓[␈↓αp, x, λyz␈↓↓:␈↓α z.y␈↓↓]]␈↓α, ␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓ ␈↓αmapcar2␈↓↓[␈↓αu, v, f␈↓↓] ← ␈↓βif n ␈↓αu ␈↓βthen ␈↓NIL ␈↓βelse f␈↓↓[␈↓βa ␈↓αu, ␈↓βa ␈↓αv␈↓↓]␈↓α . mapcar2␈↓↓[␈↓βd ␈↓αu, ␈↓βd ␈↓αv, f␈↓↓]␈↓α.␈↓
␈↓ ↓H␈↓ Getting␈αthe␈αinitial␈αposition␈αin␈αthe␈αdesired␈αform␈αis␈αas␈αcomplicated␈αa␈αcomputation␈αas␈αthe␈αactual
␈↓ ↓H␈↓tree␈α∞search.␈α∞ It␈α∞can␈α∞be␈α∞conveniently␈α∞done␈α∞by␈α∞a␈α∞sequence␈α∞of␈α∞assignment␈α∞statements␈α∞starting␈α∞with␈α
a
␈↓ ↓H␈↓description of the blocks:
␈↓ ↓H␈↓ ␈↓αpuzz1 ␈↓↓← ␈↓((G B B W R G) (G G B G W R) (G W W R B R) (G G R B W W)).
␈↓ ↓H␈↓Here␈αeach␈αblock␈αis␈αrepresented␈αby␈αa␈αlist␈αof␈αthe␈αcolors␈αof␈αthe␈αfaces␈αstarting␈αwith␈αthe␈αtop␈αface,␈αgoing
␈↓ ↓H␈↓around the sides in a clockwise direction and finishing with the bottom face.
␈↓ ↓H␈↓ We␈αneed␈αto␈αgo␈αfrom␈αthis␈αdescription␈αof␈αthe␈αblocks␈αto␈αa␈αlist␈αof␈αthe␈αpossible␈αcycles␈αof␈αcolors␈αon
␈↓ ↓H␈↓the␈α
vertical␈α
faces␈α
for␈αthe␈α
24␈α
orientations␈α
of␈αthe␈α
block.␈α
This␈α
not␈α
easy,␈αbecause␈α
the␈α
order␈α
in␈αwhich␈α
we
␈↓ ↓H␈↓have␈α∞given␈α
the␈α∞colors␈α∞is␈α
not␈α∞invariant␈α
under␈α∞rotations␈α∞of␈α
the␈α∞block.␈α
An␈α∞easy␈α∞way␈α
out␈α∞is␈α∞to␈α
start
␈↓ ↓H␈↓with␈αa␈αblock␈αwhose␈αfaces␈αare␈αassigned␈αthe␈αnumbers␈α1␈αthru␈α6␈αstarting␈αwith␈αthe␈αtop,␈αgoing␈αclockwise
␈↓ ↓H␈↓around␈αthe␈αsides␈αand␈α
finishing␈αwith␈αthe␈αbottom.␈α
We␈αwrite␈αdown␈αone␈α
cycle␈αof␈αside␈αcolors␈α
for␈αeach
␈↓ ↓H␈↓choice␈αof␈αthe␈α
face␈αput␈αon␈α
top␈αand␈αget␈αthe␈α
list␈αof␈αall␈α
24␈αcycles␈αby␈α
appending␈αthe␈αresults␈αof␈α
generating
␈↓ ↓H␈↓the cyclic permutations of the cycles. All this is accomplished by the assignment\.
␈↓ ↓H␈↓ ␈↓αpuzz2 ␈↓↓←␈↓α cycles␈↓↓[␈↓(2 3 4 5)␈↓↓]␈↓␈↓↓*␈↓␈↓αcycles␈↓↓[␈↓α(␈↓(2 5 4 3)␈↓↓]␈↓␈↓↓*␈↓ ␈↓αcycles␈↓↓[␈↓α(1 2 6 4)␈↓↓]␈↓α
␈↓ ↓H␈↓␈↓ ¬rCHAPTER II␈↓ :32
␈↓ ↓H␈↓α ␈↓↓*␈↓αcycles␈↓↓[␈↓α(1 4 6 2)␈↓↓]␈↓α␈↓↓*␈↓αcycles␈↓↓[␈↓α(1 3 6 5)␈↓↓]␈↓α␈↓↓*␈↓αcycles␈↓↓[␈↓α(1 5 6 3)␈↓↓]␈↓α, ␈↓
␈↓ ↓H␈↓where the function ␈↓αcycles␈↓ is defined by
␈↓ ↓H␈↓ ␈↓αcycles u ␈↓↓←␈↓α maplist␈↓↓[␈↓αu, λx␈↓↓:␈↓α x␈↓↓*␈↓αupto␈↓↓[␈↓αu, x␈↓↓]]␈↓
␈↓ ↓H␈↓with the auxiliary function
␈↓ ↓H␈↓ ␈↓αupto␈↓↓[␈↓αu, x␈↓↓] ← ␈↓βif ␈↓αx ␈↓βeq ␈↓αu ␈↓βthen ␈↓NIL ␈↓βelse a ␈↓αu . upto␈↓↓[␈↓βd ␈↓αu, x␈↓↓]␈↓α.␈↓
␈↓ ↓H␈↓Next␈α
we␈α
create␈α
for␈α
each␈αblock␈α
a␈α
list␈α
of␈α
substitutions␈α
expressing␈αthe␈α
colors␈α
of␈α
the␈α
six␈αnumbered␈α
faces.
␈↓ ↓H␈↓We have
␈↓ ↓H␈↓ ␈↓αpuzz3 ␈↓↓←␈↓α mapcar␈↓↓[␈↓αpuzz1, λx␈↓↓:␈↓α prup␈↓↓[␈↓(1 2 3 4 5 6), ␈↓αx␈↓↓]]␈↓α, ␈↓
␈↓ ↓H␈↓and we use these substitutions to get for each block the list of 24 orientations of the block. Thus
␈↓ ↓H␈↓ ␈↓αpuzz4 ␈↓↓←␈↓α mapcar␈↓↓[␈↓αpuzz3, λs␈↓↓:␈↓α sublis␈↓↓[␈↓αs, puzz3␈↓↓]]␈↓α.
␈↓ ↓H␈↓αpuzz4␈↓␈α
has␈αall␈α
24␈α
orientations␈αof␈α
the␈α
first␈αblock␈α
while␈αfor␈α
symmetry␈α
reasons␈αwe␈α
need␈α
only␈αconsider
␈↓ ↓H␈↓three as distinct, say the first, ninth, and seventeen. So we finally get
␈↓ ↓H␈↓ ␈↓αpuzz ␈↓↓← (␈↓βa ␈↓αnth␈↓↓[␈↓βa ␈↓αpuzz4, 1␈↓↓] ␈↓βa ␈↓αnth␈↓↓[␈↓βa ␈↓αpuzz4, 9␈↓↓] ␈↓βa ␈↓αnth␈↓↓[␈↓βa ␈↓αpuzz4, 17␈↓↓]) . ␈↓βd ␈↓αpuzz4.␈↓
␈↓ ↓H␈↓The program when compiled runs about 11 seconds on the PDP-10.
␈↓ ↓H␈↓ A␈α
more␈α
sophisticated␈α∞representation␈α
of␈α
face␈α∞cycles␈α
and␈α
partial␈α
towers␈α∞makes␈α
a␈α
factor␈α∞of␈α
ten
␈↓ ↓H␈↓speedup␈α
without␈αchanging␈α
the␈αbasic␈α
search␈αalgorithm.␈α
A␈αface␈α
cycle␈αis␈α
represented␈αby␈α
16␈αbits␈α
in␈αa
␈↓ ↓H␈↓word␈αfour␈αfor␈αeach␈αface␈αa␈α
particular␈αone␈αof␈αwhich␈αbeing␈αturned␈αon␈α
tells␈αus␈αthe␈αcolor␈αof␈αthe␈αface.␈α
If
␈↓ ↓H␈↓we␈α␈↓βor␈↓␈αthese␈αwords␈αtogether␈αfor␈αthe␈αblocks␈αin␈αa␈αpartial␈αtower␈αwe␈αget␈αa␈αword␈αwhich␈αtells␈αus␈αfor␈αeach
␈↓ ↓H␈↓face␈αof␈αthe␈αtower␈αwhat␈αcolors␈αhave␈αbeen␈αused.␈α A␈αparticular␈αface␈αcycle␈αfrom␈αthe␈αnext␈αblock␈αcan␈αbe
␈↓ ↓H␈↓added␈α⊂to␈α⊂the␈α⊂tower␈α⊂if␈α⊃the␈α⊂␈↓βand␈↓␈α⊂of␈α⊂its␈α⊂word␈α⊂with␈α⊃the␈α⊂word␈α⊂representing␈α⊂the␈α⊂tower␈α⊂is␈α⊃zero.␈α⊂ We
␈↓ ↓H␈↓represent␈αa␈αposition␈αby␈αa␈αlist␈αof␈αwords␈αrepresenting␈αits␈αpartial␈αtowers␈αwith␈α0␈αas␈αthe␈αlast␈αelement␈α
and
␈↓ ↓H␈↓the␈αhighest␈αpartial␈αtower␈α
as␈αthe␈αfirst␈αelement.␈α
The␈αvirtue␈αof␈αthis␈α
representation␈αis␈αthat␈αit␈αmakes␈α
the
␈↓ ↓H␈↓description␈αof␈αthe␈αalgorithm␈αshort.␈α The␈αinitial␈αposition␈αis␈α(0).␈α The␈αnew␈α␈↓αpuzz␈↓␈αcan␈αbe␈αformed␈αfrom
␈↓ ↓H␈↓the old one by the assignment
␈↓ ↓H␈↓ ␈↓αpuzza ␈↓↓←␈↓α mapcar␈↓↓[␈↓αpuzz, λx␈↓↓:␈↓α mapcar␈↓↓[␈↓αx, zap␈↓↓]]␈↓α, ␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓ ␈↓αzap v ␈↓↓← ␈↓βif n ␈↓αv ␈↓βthen ␈↓∧0 ␈↓βelse ␈↓αpoo ␈↓βa ␈↓αv ␈↓↓+ 16 * ␈↓αzap ␈↓βd ␈↓αv, ␈↓\.
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓αpoo x ␈↓↓← ␈↓βif ␈↓αx␈↓↓=␈↓R ␈↓βthen ␈↓α1 ␈↓βelse if ␈↓αx␈↓↓=␈↓W ␈↓βthen ␈↓2 ␈↓βelse if ␈↓αx␈↓↓=␈↓G ␈↓βthen ␈↓4 ␈↓βelse ␈↓8.
␈↓ ↓H␈↓␈↓ ¬rCHAPTER II␈↓ :33
␈↓ ↓H␈↓Now we need the new functions ␈↓αlose, ter, ␈↓and ␈↓αsuccessors␈↓. These are
␈↓ ↓H␈↓ ␈↓αlose p ␈↓↓← ␈↓βfalse␈↓α,
␈↓ ↓H␈↓α␈↓ ␈↓αter p ␈↓↓← [␈↓αlength p ␈↓↓=␈↓α 5␈↓↓]␈↓α, ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓αsuccessors p ␈↓↓←␈↓α mapchoose␈↓↓[␈↓βa ␈↓αnth␈↓↓[␈↓αpuzza, length p␈↓↓]␈↓α, λx␈↓↓:␈↓α ␈↓βa ␈↓αp ␈↓βand ␈↓αx ␈↓↓=␈↓α 0, λx␈↓↓:␈↓α ␈↓↓[␈↓βa ␈↓αp ␈↓βor ␈↓αx␈↓↓]␈↓α . p␈↓↓]␈↓α.␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓ ␈↓αmapchoose␈↓↓[␈↓αu, pred, fn␈↓↓] ← ␈↓βif n ␈↓αu ␈↓βthen ␈↓NIL
␈↓ ↓H␈↓ ␈↓βelse if ␈↓αpred ␈↓βa ␈↓αu ␈↓βthen ␈↓αfn␈↓↓[␈↓βa ␈↓αu␈↓↓]␈↓α . mapchoose␈↓↓[␈↓βd ␈↓αu , pred, fn␈↓↓] ␈↓β
␈↓ ↓H␈↓β␈↓ ␈↓βelse ␈↓αmapchoose␈↓↓[␈↓βd ␈↓αu, pred, fn␈↓↓]␈↓α.␈↓
␈↓ ↓H␈↓␈↓αlose␈↓␈α
is␈α
trivial,␈α
because␈α
the␈α
␈↓αmapchoose␈↓␈α
is␈α
used␈α
to␈α
make␈α
sure␈α
that␈α
only␈α
non-losing␈α
new␈α
positions␈α
are
␈↓ ↓H␈↓generated␈α∂by␈α∂␈↓αsuccessors␈↓.␈α∂ This␈α∂version␈α∂runs␈α∂in␈α⊂a␈α∂little␈α∂less␈α∂than␈α∂one␈α∂second␈α∂on␈α∂the␈α⊂PDP-10.␈α∂ A
␈↓ ↓H␈↓greater␈α∩speedup␈α∩can␈α⊃be␈α∩made␈α∩by␈α∩the␈α⊃application␈α∩of␈α∩some␈α⊃mathematics.␈α∩ In␈α∩fact,␈α∩with␈α⊃enough
␈↓ ↓H␈↓mathematics, extensive tree search is unnecessary in this problem.
␈↓ ↓H␈↓ ␈↓αsearch␈↓␈α∂is␈α∞used␈α∂when␈α∂we␈α∞want␈α∂to␈α∂search␈α∞a␈α∂tree␈α∂of␈α∞possibilities␈α∂for␈α∂a␈α∞solution␈α∂to␈α∂a␈α∞problem.
␈↓ ↓H␈↓Naturally␈αwe␈αcan␈αdo␈α
other␈αthings␈αwith␈αtree␈α
search␈αrecursion␈αthan␈αjust␈α
search.␈α For␈αexample␈αwe␈α
may
␈↓ ↓H␈↓want␈αto␈αfind␈α
all␈αsolutions␈αto␈αa␈α
problem.␈α This␈αcan␈αbe␈α
done␈αwith␈αa␈αfunction␈α
␈↓αallsol␈↓␈αthat␈αuses␈αthe␈α
same
␈↓ ↓H␈↓␈↓αlose, ter, ␈↓and ␈↓αsuccessors␈↓ functions as does ␈↓αsearch␈↓. The simplest way to write ␈↓αallsol␈↓ is
␈↓ ↓H␈↓ ␈↓αallsol p ␈↓↓← ␈↓βif ␈↓αlose p ␈↓βthen ␈↓NIL ␈↓βelse if ␈↓αter p ␈↓βthen ␈↓α(p) ␈↓βelse ␈↓αmapapp␈↓↓[␈↓αsuccessors p, allsol␈↓↓]␈↓α, ␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓ ␈↓αmapapp␈↓↓[␈↓αu, fn␈↓↓] ← ␈↓βif n ␈↓αu ␈↓βthen ␈↓NIL ␈↓βelse ␈↓αfn␈↓↓[␈↓βa ␈↓αu␈↓↓]␈↓α . mappap␈↓↓[␈↓βd ␈↓αu, fn␈↓↓]␈↓α.␈↓
␈↓ ↓H␈↓This␈αform␈αof␈α
the␈αfunction␈αis␈αsomewhat␈α
inefficient␈αbecause␈αof␈αall␈α
the␈α␈↓αappend␈↓ing.␈α A␈α
more␈αefficient
␈↓ ↓H␈↓form uses an auxiliary function as follows:
␈↓ ↓H␈↓ ␈↓αallsol p ␈↓↓←␈↓α allsola␈↓↓[␈↓αp, ␈↓NIL␈↓↓]␈↓
␈↓ ↓H␈↓ ␈↓αallsola␈↓↓[␈↓αp, found␈↓↓] ← ␈↓βif ␈↓αlose p ␈↓βthen ␈↓αfound
␈↓ ↓H␈↓α ␈↓βelse if ␈↓αter p ␈↓βthen ␈↓αp . found
␈↓ ↓H␈↓α ␈↓βelse ␈↓αallsolb␈↓↓[␈↓αsuccessors p, found␈↓↓]␈↓α,
␈↓ ↓H␈↓α␈↓ ␈↓αallsolb␈↓↓[␈↓αu, found␈↓↓] ← ␈↓βif n ␈↓αu ␈↓βthen ␈↓αfound ␈↓βelse ␈↓αallsolb␈↓↓[␈↓βd ␈↓αu, allsola␈↓↓[␈↓βa ␈↓αu, found␈↓↓]]␈↓α.␈↓
␈↓ ↓H␈↓The␈α∩recursive␈α⊃program␈α∩structure␈α⊃that␈α∩arises␈α⊃here␈α∩is␈α∩common␈α⊃when␈α∩a␈α⊃list␈α∩is␈α⊃to␈α∩be␈α∩formed␈α⊃by
␈↓ ↓H␈↓recurring through a list structure.
␈↓ ↓H␈↓␈↓ ¬rCHAPTER II␈↓ :34
␈↓ ↓H␈↓6. ␈↓βGame trees.␈↓
␈↓ ↓H␈↓ The␈α∞positions␈α∞that␈α∞can␈α∞be␈α∞reached␈α∞from␈α∞an␈α∞initial␈α∞position␈α∞in␈α∞a␈α∞ game␈α∞ form␈α∞ a␈α∂ tree,␈α∞and
␈↓ ↓H␈↓deciding␈αwhat␈αmove␈αto␈αmake␈αoften␈αinvolves␈αsearching␈αthis␈αtree.␈α However,␈αgame␈αtrees␈αare␈αsearched
␈↓ ↓H␈↓in␈αa␈αdifferent␈αway␈α than␈αthe␈αtrees␈αwe␈αhave␈αlooked␈αat,␈αbecause␈αthe␈αopposing␈αinterests␈αof␈αthe␈αplayers
␈↓ ↓H␈↓makes␈α
it␈α
not␈αa␈α
search␈α
for␈α
a␈αjoint␈α
line␈α
of␈α
play␈α that␈α
will␈α
lead␈α
to␈α the␈α
first␈α
player␈α
winning,␈αbut
␈↓ ↓H␈↓rather a search for a strategy that will lead to a win regardless of what the other player does.
␈↓ ↓H␈↓ The␈α simplest␈α situation␈α is␈α characterized␈α by␈α a␈α function␈α␈↓αsuccessors␈↓␈αthat␈αgives␈αthe␈αpositions
␈↓ ↓H␈↓that␈αcan␈αbe␈αreached␈αin␈α one␈αmove,␈α a␈α predicate␈α ␈↓αter␈↓␈α that␈α tells␈α when␈αa␈αposition␈αis␈αto␈αbe␈αregarded
␈↓ ↓H␈↓as␈α_ terminal␈α↔ for␈α_ the␈α_ given␈α↔ analysis,␈α_ and␈α↔ a␈α_ function␈α_␈↓αimval␈↓␈α↔ that␈α_ gives␈α_ a␈α↔ number
␈↓ ↓H␈↓approximating␈α the␈α value␈α of␈αthe␈α
position␈αto␈αone␈αof␈αthe␈α
players.␈α We␈α shall␈α call␈α this␈α player␈α
the
␈↓ ↓H␈↓maximizing␈α player␈α and␈α his␈αopponent␈αthe␈αminimizing␈αplayer.␈αUsually,␈αthe␈αnumerical␈αvalues␈αarise,
␈↓ ↓H␈↓because␈α∂the␈α⊂search␈α∂cannot␈α⊂be␈α∂carried␈α∂ out␈α⊂to␈α∂the␈α⊂end␈α∂of␈α∂the␈α⊂game,␈α∂and␈α⊂the␈α∂analysis␈α⊂stops␈α∂with
␈↓ ↓H␈↓reasonably␈αstatic␈α
positions␈αthat␈α can␈α
be␈α evaluated␈α
by␈α some␈α rule.␈α
Naturally,␈α the␈αfunction␈α
␈↓αimval␈↓
␈↓ ↓H␈↓is␈α
chosen␈α to␈α
be␈α
easy␈α to␈α
calculate␈αand␈α
to␈α
correlate␈αwell␈α
with␈α
the␈αprobability␈α
that␈αthe␈α
maximizing
␈↓ ↓H␈↓player can win the position.
␈↓ ↓H␈↓ The␈αsimplest␈αrule␈αfor␈αfinding␈αthe␈αcorrect␈αmove␈αin␈αa␈αposition␈αuses␈αauxiliary␈αfunctions␈α␈↓αvalmax␈↓
␈↓ ↓H␈↓and␈α␈↓αvalmin␈↓␈α
that␈αgive␈αa␈α
value␈αto␈αa␈α
position␈αby␈α
using␈α␈↓αimval␈↓␈αif␈α
the␈αposition␈αis␈α
terminal␈αand␈αtaking␈α
the
␈↓ ↓H␈↓max or min of the successor positions otherwise.
␈↓ ↓H␈↓ For␈αthis␈αwe␈αwant␈αfunctions␈αfor␈αgetting␈αthe␈αmaximum␈αor␈αthe␈αminimum␈αof␈αa␈αfunction␈αon␈αa␈α
list,
␈↓ ↓H␈↓and they are defined as follows:
␈↓ ↓H␈↓ ␈↓αmaxlis␈↓↓[␈↓αu, f␈↓↓] ← ␈↓βif n ␈↓αu ␈↓βthen ␈↓α-␈↓↓∞␈↓α ␈↓βelse ␈↓α max␈↓↓[␈↓αf␈↓↓[␈↓βa ␈↓αu␈↓↓]␈↓α, maxlis␈↓↓[␈↓βd ␈↓αu, f␈↓↓]]␈↓α, ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓αminlis␈↓↓[␈↓αu, f␈↓↓] ← ␈↓βif n ␈↓αu ␈↓βthen ␈↓α␈↓↓∞␈↓α ␈↓βelse ␈↓α min␈↓↓[␈↓αf␈↓↓[␈↓βa ␈↓αu␈↓↓]␈↓α, minlis␈↓↓[␈↓βd ␈↓αu, f␈↓↓]]␈↓α.␈↓
␈↓ ↓H␈↓In␈α
these␈α
functions,␈α-␈↓↓∞␈↓␈α
and␈α
␈↓↓∞␈↓␈αrepresent␈α
numbers␈α
that␈αare␈α
smaller␈α
and␈αlarger␈α
than␈α
any␈αactual␈α
values
␈↓ ↓H␈↓that␈αwill␈α
occur␈αin␈α
evaluating␈α␈↓αf␈↓.␈α
If␈αthese␈α
numbers␈αare␈α
not␈αavailable,␈α
then␈αit␈α
is␈αnecessary␈α
to␈αprime␈α
the
␈↓ ↓H␈↓function␈α∩with␈α∩the␈α∩values␈α∩of␈α∩␈↓αf␈↓␈α∩applied␈α∩to␈α∪the␈α∩first␈α∩element␈α∩of␈α∩the␈α∩list,␈α∩and␈α∩the␈α∪functions␈α∩are
␈↓ ↓H␈↓meaningless␈αfor␈αnull␈αlists.␈α Iterative␈αversions␈αof␈αthe␈αfunctions␈αare␈αgenerally␈αbetter;␈αwe␈αgive␈αonly␈αthe
␈↓ ↓H␈↓iterative version of ␈↓αmaxlis␈↓, namely
␈↓ ↓H␈↓ ␈↓αmaxlis␈↓↓[␈↓αu, f␈↓↓] ←␈↓α maxlisa␈↓↓[␈↓αu, -␈↓↓∞␈↓α, f␈↓↓]␈↓α, ␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓ ␈↓αmaxlisa␈↓↓[␈↓αu, x, f␈↓↓] ← ␈↓αif n u ␈↓βthen ␈↓αx ␈↓βelse ␈↓αmaxlisa␈↓↓[␈↓βd ␈↓αu, max␈↓↓[␈↓αx, f␈↓↓[␈↓βa ␈↓αu␈↓↓]]␈↓α, f␈↓↓]␈↓α.␈↓
␈↓ ↓H␈↓We now have
␈↓ ↓H␈↓ ␈↓αvalmax p ␈↓↓← ␈↓βif ␈↓αter p ␈↓βthen ␈↓αimval p ␈↓βelse ␈↓αmaxlis␈↓↓[␈↓αsuccessors p, valmin␈↓↓]␈↓,
␈↓ ↓H␈↓␈↓ ¬rCHAPTER II␈↓ :35
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓αvalmin p ␈↓↓← ␈↓βif ␈↓αter p ␈↓βthen ␈↓αimval p ␈↓βelse ␈↓αminlis␈↓↓[␈↓αsuccessors p, valmax␈↓↓]␈↓.
␈↓ ↓H␈↓The best move is now chosen by
␈↓ ↓H␈↓ ␈↓αmovemax p ␈↓↓←␈↓α bestmax␈↓↓[␈↓αsuccesors p, valmin␈↓↓]␈↓α, ␈↓
␈↓ ↓H␈↓or
␈↓ ↓H␈↓ ␈↓αmovemin p ␈↓↓←␈↓α bestmin␈↓↓[␈↓αsuccesors p, valmax␈↓↓]␈↓α, ␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓ ␈↓αbestmax␈↓↓[␈↓αu, f␈↓↓] ←␈↓α bestmaxa␈↓↓[␈↓βd ␈↓αu, ␈↓βa ␈↓αu, f␈↓↓[␈↓βa ␈↓αu␈↓↓]␈↓α, f␈↓↓]␈↓α,
␈↓ ↓H␈↓α␈↓ ␈↓αbestmaxa␈↓↓[␈↓αu, sofar, valsofar, f␈↓↓] ← ␈↓βif n ␈↓αu ␈↓βthen ␈↓αsofar
␈↓ ↓H␈↓α␈↓ ␈↓α␈↓βelse if ␈↓αf␈↓↓[␈↓βa ␈↓αu␈↓↓] ≤ ␈↓αbestsofar ␈↓βthen ␈↓αbestmaxa␈↓↓[␈↓βd ␈↓αu, sofar, bestsofar, f␈↓↓]␈↓α
␈↓ ↓H␈↓α␈↓ ␈↓α␈↓βelse ␈↓αbestmaxa␈↓↓[␈↓βd ␈↓αu, ␈↓βa ␈↓αu, f␈↓↓[␈↓βa ␈↓αu␈↓↓]␈↓α, f␈↓↓]␈↓α,
␈↓ ↓H␈↓α␈↓ ␈↓αbestmin␈↓↓[␈↓αu, f␈↓↓] ←␈↓α bestmina␈↓↓[␈↓βd ␈↓αu, ␈↓βa ␈↓αu, f␈↓↓[␈↓βa ␈↓αu␈↓↓]␈↓α, f␈↓↓]␈↓α, ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓αbestmina␈↓↓[␈↓αu, sofar, valsofar, f␈↓↓] ← ␈↓βif n ␈↓αu ␈↓βthen ␈↓αsofar
␈↓ ↓H␈↓α␈↓ ␈↓α␈↓βelse if ␈↓αf␈↓↓[␈↓βa ␈↓αu␈↓↓] ≥ ␈↓αbestsofar ␈↓βthen ␈↓αbestmina␈↓↓[␈↓βd ␈↓αu, sofar, bestsofar, f␈↓↓]␈↓α
␈↓ ↓H␈↓α␈↓ ␈↓α␈↓βelse ␈↓αbestmina␈↓↓[␈↓βd ␈↓αu, ␈↓βa ␈↓αu, f␈↓↓[␈↓βa ␈↓αu␈↓↓]␈↓α, f␈↓↓]␈↓α.␈↓
␈↓ ↓H␈↓ However,␈αthis␈αstraight␈αminimax␈αcomputation␈αof␈αthe␈αbest␈αmove␈αdoes␈αmuch␈αmore␈αcomputation
␈↓ ↓H␈↓in␈αgeneral␈αthan␈αis␈αusually␈αnecessary.␈α The␈αsimplest␈αway␈αto␈αsee␈αthis␈αis␈αfrom␈αthe␈αmove␈αtree␈αof␈αFigure
␈↓ ↓H␈↓2.6.
␈↓"␈↓ ↓H␈↓␈↓ ¬H ≤ 8
␈↓"␈↓ ↓H␈↓␈↓ ¬H max ≤'
␈↓"␈↓ ↓H␈↓␈↓ ¬H '␈↓ ↓H αααα 1␈↓ ↓H ≤≥
␈↓"␈↓ ↓H␈↓␈↓ ¬H min ≤' `≥
␈↓"␈↓ ↓H␈↓␈↓ ¬H ≤' ` 6
␈↓"␈↓ ↓H␈↓␈↓ ¬H '␈↓ ↓H ≥
␈↓"␈↓ ↓H␈↓␈↓ ¬H `≥ ≤ 9
␈↓"␈↓ ↓H␈↓␈↓ ¬H `≥ ≤'
␈↓"␈↓ ↓H␈↓␈↓ ¬H `'␈↓ ↓H αααα X␈↓ ↓H ≥
␈↓"␈↓ ↓H␈↓␈↓ ¬H `≥
␈↓"␈↓ ↓H␈↓␈↓ ¬H ` X
␈↓ ↓H␈↓␈↓ ¬rCHAPTER II␈↓ :36
␈↓ ↓H␈↓↔␈↓Figure 2.6␈↓
␈↓ ↓H␈↓We␈α
see␈αfrom␈α
this␈α
figure␈αthat␈α
it␈α
is␈αunnecessary␈α
to␈α
examine␈αthe␈α
moves␈α
marked␈αx,␈α
because␈αtheir␈α
values
␈↓ ↓H␈↓have␈α∂no␈α∞effect␈α∂on␈α∂the␈α∞value␈α∂of␈α∂the␈α∞game␈α∂or␈α∂on␈α∞the␈α∂correct␈α∂move␈α∞since␈α∂the␈α∂presence␈α∞of␈α∂the␈α∂9␈α∞is
␈↓ ↓H␈↓sufficient␈αinformation␈α
at␈αthis␈α
point␈αto␈α
show␈αthat␈αthe␈α
move␈αleading␈α
to␈αthe␈α
vertex␈αpreceding␈αit␈α
should
␈↓ ↓H␈↓not be chosen by the minimizing player.
␈↓ ↓H␈↓ The␈αgeneral␈αsituation␈αis␈αthat␈αit␈αis␈αunnecessary␈αto␈αexamine␈αfurther␈αmoves␈αin␈αa␈αposition␈αonce␈αa
␈↓ ↓H␈↓move␈α
is␈α
found␈α
that␈α
refutes␈α∞moving␈α
to␈α
the␈α
position␈α
in␈α
the␈α∞first␈α
place.␈α
This␈α
idea␈α
is␈α
called␈α∞the␈α
αβ-
␈↓ ↓H␈↓heuristic and expressed in the following recursive definitions:
␈↓ ↓H␈↓ ␈↓αmaxval p ␈↓↓←␈↓α maxval1␈↓↓[␈↓αp, -␈↓↓∞␈↓α, ␈↓↓∞␈↓α␈↓↓]␈↓α,
␈↓ ↓H␈↓α maxval1␈↓↓[␈↓αp,␈αα,␈α
β␈↓↓]␈α←␈α␈↓βif␈α
␈↓αter␈αp␈α␈↓βthen␈α
␈↓αmax␈↓↓[␈↓αα,␈αmin␈↓↓[␈↓αβ,␈αimval␈α
p␈↓↓]]␈↓α␈α ␈↓βelse␈α␈↓αmaxval2␈↓↓[␈↓αsuccessors␈α
p,␈αα,
␈↓ ↓H␈↓αβ␈↓↓]␈↓α,
␈↓ ↓H␈↓α maxval2␈↓↓[␈↓αu, α, β␈↓↓] ← {␈↓αminval1␈↓↓[␈↓βa ␈↓αu, α, β␈↓↓]}[␈↓αλw␈↓↓:␈↓α ␈↓βif ␈↓αw ␈↓↓=␈↓α β ␈↓βthen ␈↓αβ ␈↓βelse ␈↓αmaxval2␈↓↓[␈↓βd ␈↓αu, w, β␈↓↓]]␈↓α,
␈↓ ↓H␈↓α␈↓ ␈↓αminval p ␈↓↓←␈↓α minval1␈↓↓[␈↓αp, -␈↓↓∞␈↓α, ␈↓↓∞␈↓α␈↓↓]␈↓α,
␈↓ ↓H␈↓α minval1␈↓↓[␈↓αp,␈α
α,␈α
β␈↓↓]␈α
←␈α
␈↓βif␈α
␈↓αter␈α
p␈α␈↓βthen␈α
␈↓αmax␈↓↓[␈↓αα,␈α
min␈↓↓[␈↓αβ,␈α
imval␈α
p␈↓↓]]␈↓α␈α
␈↓βelse␈α
␈↓αminval2␈↓↓[␈↓αsuccessors␈α
p,␈αα,
␈↓ ↓H␈↓αβ␈↓↓]␈↓α,
␈↓ ↓H␈↓α minval2␈↓↓[␈↓αu, α, β␈↓↓] ← {␈↓αmaxval1␈↓↓[␈↓βa ␈↓αu, α, β␈↓↓]}[␈↓αλw␈↓↓:␈↓α ␈↓βif ␈↓αw ␈↓↓=␈↓α α ␈↓βthen ␈↓αα ␈↓βelse ␈↓αminval2␈↓↓[␈↓βd ␈↓αu, α, w␈↓↓]␈↓α.␈↓
␈↓ ↓H␈↓ The␈αreduction␈α
in␈αnumber␈αof␈α
positions␈αexamined␈αgiven␈α
by␈αthe␈ααβ␈α
algorithm␈αover␈α
the␈αsimple
␈↓ ↓H␈↓minimax␈αalgorithm␈αdepends␈αon␈α
the␈αorder␈αin␈αwhich␈α
the␈αmoves␈αare␈αexamined.␈α
In␈αthe␈αworst␈αcase,␈α
the
␈↓ ↓H␈↓moves␈αhappen␈αto␈αbe␈αexamined␈αin␈αinverse␈αorder␈αof␈α
merit␈αin␈αevery␈αposition␈αon␈αthe␈αtree,␈αi.e.␈αthe␈α
worst
␈↓ ↓H␈↓move␈α
first.␈α
In␈α
that␈αcase,␈α
there␈α
is␈α
no␈αimprovement␈α
over␈α
minimax.␈α
The␈α
best␈αcase␈α
is␈α
the␈α
one␈αin␈α
which
␈↓ ↓H␈↓the␈α
best␈α
move␈α
in␈αevery␈α
position␈α
is␈α
examined␈αfirst.␈α
If␈α
we␈α
look␈α␈↓αn␈↓␈α
moves␈α
deep␈α
on␈αa␈α
tree␈α
that␈α
has␈α␈↓αk␈↓
␈↓ ↓H␈↓successors␈α
to␈α
each␈α
position,␈α
then␈α
minimax␈α
looks␈α
at␈α
␈↓αk␈↓εn␈↓␈α
positions␈α
while␈α
αβ␈α
looks␈α
at␈α
about␈α
only␈α␈↓αk␈↓εn/2␈↓.
␈↓ ↓H␈↓Thus␈α
a␈α
program␈α
that␈α
looks␈α
at␈α
10␈↓ε4␈↓␈α
moves␈α
with␈α
αβ␈α
might␈α
have␈α
to␈α
look␈α
at␈α
10␈↓ε8␈↓␈α
with␈α
minimax.␈α
For
␈↓ ↓H␈↓this␈α
reason,␈α
game␈α
playing␈αprograms␈α
using␈α
αβ␈α
make␈αa␈α
big␈α
effort␈α
to␈αinclude␈α
as␈α
good␈α
as␈α
possible␈αan
␈↓ ↓H␈↓ordering␈α
of␈αmoves␈α
into␈α
the␈α␈↓αsuccessors␈↓␈α
function.␈α
When␈αthere␈α
is␈α
a␈αdeep␈α
tree␈α
search␈αto␈α
be␈α
done,␈αthe
␈↓ ↓H␈↓way␈α∂to␈α⊂make␈α∂the␈α⊂ordering␈α∂is␈α∂with␈α⊂a␈α∂short␈α⊂look-ahead␈α∂to␈α∂a␈α⊂much␈α∂smaller␈α⊂depth␈α∂than␈α⊂the␈α∂main
␈↓ ↓H␈↓search.␈α∞ Still␈α∞shorter␈α∞look-aheads␈α∞are␈α∞used␈α∞deeper␈α∂in␈α∞the␈α∞tree,␈α∞and␈α∞beyond␈α∞a␈α∞certain␈α∂depth,␈α∞non-
␈↓ ↓H␈↓look-ahead ordering methods are of decreasing complexity.
␈↓ ↓H␈↓ A␈α∂version␈α∞of␈α∂αβ␈α∞incorporating␈α∂optimistic␈α∞and␈α∂pessimistic␈α∞evaluations␈α∂of␈α∞positions␈α∂was␈α∞first
␈↓ ↓H␈↓proposed␈α⊂by␈α⊂McCarthy␈α⊂about␈α⊂1958.␈α⊂ Edwards␈α⊂and␈α⊂Hart␈α⊂at␈α⊂M.I.T.␈α⊂about␈α⊂1959␈α⊂proved␈α⊃that␈α⊂the
␈↓ ↓H␈↓present␈α∂version␈α⊂of␈α∂αβ␈α∂works␈α⊂and␈α∂calculated␈α∂the␈α⊂improvement␈α∂it␈α∂gives␈α⊂over␈α∂minimax.␈α⊂ The␈α∂first
␈↓ ↓H␈↓publication,␈α
however,␈α
was␈α
Brudno␈α
(1963).␈α
It␈α
is␈α
worth␈α
noting␈α
that␈α
αβ␈α
was␈α
not␈α
used␈α
in␈α
the␈α
early␈α
chess
␈↓ ↓H␈↓playing␈α⊂programs␈α⊃in␈α⊂spite␈α⊃of␈α⊂the␈α⊂fact␈α⊃that␈α⊂it␈α⊃is␈α⊂clearly␈α⊂used␈α⊃in␈α⊂any␈α⊃human␈α⊂play.␈α⊃ Its␈α⊂non-use,
␈↓ ↓H␈↓therefore,␈α∪represents␈α∪a␈α∪failure␈α∪of␈α∪self-observation.␈α∪ Very␈α∪likely,␈α∪there␈α∪are␈α∪a␈α∪number␈α∪of␈α∩other
␈↓ ↓H␈↓algorithms␈αused␈αin␈αhuman␈αthought␈αthat␈αwe␈α
have␈αnot␈αnoticed␈αan␈αincorporated␈αin␈αour␈αprograms.␈α
To
␈↓ ↓H␈↓␈↓ ¬rCHAPTER II␈↓ :37
␈↓ ↓H␈↓the␈αextent␈α
that␈αthis␈α
is␈αso,␈α
artificial␈αintelligence␈α
will␈αbe␈α
␈↓αa␈αposteriori␈↓␈α
obvious␈αeven␈α
though␈αit␈α
is␈α␈↓αa␈α
priori␈↓
␈↓ ↓H␈↓very difficult.
␈↓ ↓H␈↓␈↓ εP␈↓ :38
␈↓ ↓H␈↓β␈↓ ¬lCHAPTER III
␈↓ ↓H␈↓β␈↓ ¬7COMPILING IN LISP
␈↓ ↓H␈↓1. ␈↓βIntroduction␈↓
␈↓ ↓H␈↓ Compiling␈α
is␈α
an␈α
important␈α
example␈α
of␈α
symbolic␈α
computation␈α
and␈α
has␈α
received␈α
much␈αstudy.
␈↓ ↓H␈↓Much␈αof␈αthe␈αstudy␈αhas␈αbeen␈αdevoted␈αto␈αparsing␈αwhich␈αis␈αessentially␈αthe␈αtransformation␈αof␈αan␈αinput
␈↓ ↓H␈↓string␈α⊃in␈α⊃the␈α⊂source␈α⊃language␈α⊃into␈α⊃an␈α⊂internal␈α⊃form.␈α⊃ The␈α⊃internal␈α⊂form␈α⊃used␈α⊃depends␈α⊃on␈α⊂the
␈↓ ↓H␈↓compiler.␈α⊃ Sometimes␈α⊃it␈α⊃is␈α⊃Polish␈α⊃prefix␈α⊃or␈α⊃postfix␈α⊃notation,␈α⊃sometimes␈α⊃it␈α⊃is␈α⊃list␈α⊃structure,␈α⊃and
␈↓ ↓H␈↓sometimes it consists of entries in a collection of tables.
␈↓ ↓H␈↓ When␈α
internal␈α
notation␈α
LISP␈α
is␈α
being␈α
compiled,␈αthe␈α
parsing␈α
is␈α
trivial,␈α
because␈α
the␈α
input␈αis␈α
S-
␈↓ ↓H␈↓expressions␈αand␈αthe␈α
internal␈αform␈αwanted␈α
is␈αlist␈αstructure,␈αso␈α
what␈αparsing␈αthere␈α
is␈αis␈αdone␈α
by␈αthe
␈↓ ↓H␈↓ordinary␈α∀LISP␈α∀␈↓αread␈↓␈α∀routine.␈α∀ Therefore,␈α∀compilers␈α∀can␈α∀be␈α∀very␈α∀compact␈α∀and␈α∃transparent␈α∀in
␈↓ ↓H␈↓structure,␈αand␈αwe␈αcan␈αconcentrate␈αour␈αattention␈αon␈αthe␈αcode␈αgeneration␈αphase.␈α This␈αis␈αas␈αit␈αshould
␈↓ ↓H␈↓be,␈α
because,␈αin␈α
my␈αopinion,␈α
parsing␈α
is␈αbasically␈α
a␈αside␈α
issue␈α
in␈αcompiling,␈α
and␈αcode␈α
generation␈αis␈α
the
␈↓ ↓H␈↓matter of main scientific interest.
␈↓ ↓H␈↓ We␈αshall␈αdescribe␈αtwo␈αcompilers␈αin␈αthis␈αchapter␈αcalled␈αLCOM0␈αand␈αLCOM4␈αwhich␈αcompile
␈↓ ↓H␈↓S-expression␈αLISP␈αinto␈αmachine␈αlanguage␈αfor␈αthe␈αPDP-10␈αcomputer␈αaccording␈αto␈α
the␈αconventions
␈↓ ↓H␈↓of␈αStanford␈αLISP␈α1.6.␈α For␈αnow␈αwe␈αshall␈αtake␈αthese␈αconventions␈αfor␈αgranted.␈α Before␈αdescribing␈αthe
␈↓ ↓H␈↓compilers, we must describe these conventions.
␈↓ ↓H␈↓ The␈α
target␈α
language␈αis␈α
called␈α
LAP␈αfor␈α
LISP␈α
assembly␈αprogram.␈α
Each␈α
function␈α
is␈αcompiled
␈↓ ↓H␈↓separately␈α
into␈α
a␈α
separate␈α
LAP␈α
program,␈α
and␈α
these␈α
programs␈α
are␈α
written␈α
onto␈α
a␈α
disk␈α∞file.␈α
These
␈↓ ↓H␈↓files␈αcan␈α
later␈αbe␈α
read␈αinto␈α
a␈αLISP␈α
core␈αimage␈α
and␈αassembled␈α
in␈αplace␈α
by␈αthe␈α
LAP␈αassembler␈α
which
␈↓ ↓H␈↓is␈α
part␈α∞of␈α
the␈α
LISP␈α∞runtime␈α
routines.␈α
The␈α∞compiler,␈α
on␈α
the␈α∞other␈α
hand,␈α
is␈α∞a␈α
separate␈α∞LISP␈α
core
␈↓ ↓H␈↓image␈α∂that␈α∂can␈α∂be␈α∞instructed␈α∂to␈α∂compile␈α∂several␈α∞input␈α∂files.␈α∂ For␈α∂an␈α∞input␈α∂file␈α∂called␈α∂<name>,␈α∞it
␈↓ ↓H␈↓produces␈α⊂and␈α⊂output␈α⊂file␈α⊂called␈α⊂<name>.LAP.␈α⊃ All␈α⊂this␈α⊂is␈α⊂specific␈α⊂to␈α⊂the␈α⊃PDP-10␈α⊂time-sharing
␈↓ ↓H␈↓system␈α
and␈α
fortunately␈α
works␈α
the␈α
same␈α∞whether␈α
the␈α
time-sharing␈α
system␈α
is␈α
the␈α
D.E.C.␈α∞system,␈α
the
␈↓ ↓H␈↓Stanford system, or TENEX.
␈↓ ↓H␈↓ The␈αLAP␈αprogram␈αproduced␈αis␈αa␈αlist␈αof␈αwords;␈αthe␈αlast␈αword␈αis␈αNIL,␈αand␈αthe␈αfirst␈αword␈αis␈αa
␈↓ ↓H␈↓header␈αof␈αthe␈αform␈α(LAP␈α␈↓αfname␈↓␈αSUBR)␈αwhere␈α␈↓αfname␈↓␈αis␈αthe␈αname␈αof␈αthe␈αfunction␈αcompiled.␈α This
␈↓ ↓H␈↓header␈α
tells␈α
the␈α
DSKIN␈α
program␈α
that␈α
it␈α
should␈α
read␈α
S-expressions␈α
till␈α
it␈α
comes␈α
to␈α
NIL␈α
and␈α
then
␈↓ ↓H␈↓submit␈α
what␈αit␈α
has␈αcollected␈α
to␈αthe␈α
LAP␈αassembler␈α
which␈α
will␈αassemble␈α
it␈αinto␈α
core.␈α The␈α
rest␈αof␈α
the
␈↓ ↓H␈↓words are either atoms representing labels or lists representing single PDP-10 instructions.
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :39
␈↓ ↓H␈↓2. ␈↓βSome facts about the PDP-10.␈↓
␈↓ ↓H␈↓ The following facts about the PDP-10 may be of use:
␈↓ ↓H␈↓ The␈αPDP-10␈αhas␈αa␈α36␈αbit␈αword␈αand␈αan␈α18␈αbit␈αaddress.␈α In␈αinstructions␈αand␈αin␈αaccumulators
␈↓ ↓H␈↓used␈α
as␈α
index␈α
registers␈α
the␈α
address␈α
is␈α
found␈α
in␈α
the␈α
right␈α
part␈α
of␈α
the␈α
word␈α
where␈α
the␈αleast␈α
significant
␈↓ ↓H␈↓bits in arithmetic reside.
␈↓ ↓H␈↓ There␈α∞are␈α∂16␈α∞general␈α∞registers␈α∂which␈α∞serve␈α∞simultaneously␈α∂ as␈α∞accumulators␈α∂ (receiving␈α∞the
␈↓ ↓H␈↓results␈α
of␈α
arithmetic␈α
operations),␈α
index␈α
registers␈α
(modifying␈α
the␈α
nominal␈α
addresses␈α∞of␈α
instructions
␈↓ ↓H␈↓to␈α form␈αeffective␈αaddresses),␈αand␈αas␈αthe␈αfirst␈α16␈αregisters␈αof␈αmemory␈α(if␈αthe␈αeffective␈αaddress␈αof␈α
an
␈↓ ↓H␈↓instruction␈α∞ is␈α
less␈α∞ than␈α∞ 16,␈α
then␈α∞ the␈α∞instruction␈α
uses␈α∞the␈α∞corresponding␈α
general␈α∞register␈α∞as␈α
its
␈↓ ↓H␈↓operand.)
␈↓ ↓H␈↓ All␈αinstructions␈αhave␈αthe␈αsame␈αformat␈αand␈αare␈αwritten␈αfor␈αthe␈αLAP␈αassembly␈αprogram␈αin␈αthe
␈↓ ↓H␈↓form
␈↓ ↓H␈↓ (<op name> <accumulator> <address> <index register>).
␈↓ ↓H␈↓Thus␈α (MOVE␈α1␈α3␈αP) ␈αcauses␈αaccumulator␈α 1 ␈αto␈αreceive␈αthe␈αcontents␈αof␈αa␈α memory␈α register␈αwhose
␈↓ ↓H␈↓address␈αis␈α 3+c(P),␈αi.e.␈α3+<the␈αcontents␈αof␈αgeneral␈αregister␈αP>.␈α If␈αno␈αindex␈αregister␈αis␈αspecified,␈αthen
␈↓ ↓H␈↓the␈αaddress␈αused␈αis␈αjust␈α<address>.␈α If␈αthe␈αop␈αname␈αis␈αfollowed␈αby␈α @,␈αthen␈αthe␈αmemory␈αreference␈αis
␈↓ ↓H␈↓indirect,␈αi.e.␈αthe␈αeffective␈αaddrress␈αis␈αthe␈αcontents␈αof␈αthe␈αright␈αhalf␈αof␈αthe␈αword␈α directly␈α addressed
␈↓ ↓H␈↓by␈α∩ the␈α∩ instruction␈α∩(modified␈α∩ by␈α∩ the␈α∩index␈α∩register␈α∩and␈α∩indirect␈α∩bit␈α∩of␈α∩that␈α∩word).␈α∩ In␈α⊃the
␈↓ ↓H␈↓following␈α⊂description␈α⊂of␈α⊂some␈α⊂instructions␈α⊃useful␈α⊂in␈α⊂constructing␈α⊂the␈α⊂compiler,␈α⊂<ef>␈α⊃denotes␈α⊂the
␈↓ ↓H␈↓effective address of an instruction.
␈↓ ↓H␈↓␈↓ αHMOVE ␈↓ ¬((ac) ← c(<ef>)
␈↓ ↓H␈↓␈↓ αHMOVEI ␈↓ ¬(c(ac) ← <ef>
␈↓ ↓H␈↓␈↓ αHMOVEM ␈↓ ¬(c(<ef>) ← c(ac)
␈↓ ↓H␈↓␈↓ αHHLRZ ␈↓ ¬(right half of c(ac) ← left half of c(<ef>)
␈↓ ↓H␈↓␈↓ αHHRRZ ␈↓ ¬(right half of c(ac) ← right half of c(<ef>)
␈↓ ↓H␈↓␈↓ αH␈↓ β8(These two are used indirectly for car and cdr respectively.)
␈↓ ↓H␈↓␈↓ αHADD ␈↓ ¬(c(ac) ← c(ac) + c(<ef>)
␈↓ ↓H␈↓␈↓ αHSUB ␈↓ ¬(c(ac) ← c(ac) - c(<ef>)
␈↓ ↓H␈↓␈↓ αHJRST ␈↓ ¬(go to <ef>
␈↓ ↓H␈↓␈↓ αHJUMPE ␈↓ ¬(if c(ac) ␈↓↓=␈↓ 0 then go to <ef>
␈↓ ↓H␈↓␈↓ αHJUMPN ␈↓ ¬(if c(ac) ␈↓↓≠␈↓ 0 then go to <ef>
␈↓ ↓H␈↓␈↓ αHCAME ␈↓ ¬(if c(ac) ␈↓↓=␈↓ c(<ef>) then <skip next instruction>
␈↓ ↓H␈↓␈↓ αHCAMN ␈↓ ¬(if c(ac) ␈↓↓≠␈↓ c(<ef>) then <skip next instruction>
␈↓ ↓H␈↓␈↓ αHPUSH ␈↓ ¬(c(c(right␈α∞half␈α∞of␈α
ac))␈α∞←␈α∞c(<ef>);␈α
the␈α∞contents␈α∞of␈α
each
␈↓ ↓H␈↓␈↓ ¬(half␈αof␈αac␈αis␈αincreased␈αby␈αone␈αand␈αif␈αthe␈αcontents␈αof
␈↓ ↓H␈↓␈↓ ¬(the␈α⊂left␈α⊂half␈α⊂is␈α∂then␈α⊂ 0,␈α⊂a␈α⊂stack␈α⊂overflow␈α∂interrupt
␈↓ ↓H␈↓␈↓ ¬(occurs.␈α (PUSH␈αP␈αac)␈αis␈αis␈αused␈αin␈αLISP␈αto␈αput␈αc(ac)
␈↓ ↓H␈↓␈↓ ¬(on the stack.
␈↓ ↓H␈↓␈↓ αHPOP ␈↓ ¬(c(<ef>)␈α∂←c(c(right␈α⊂half␈α∂of␈α∂ac));␈α⊂the␈α∂contents␈α⊂of␈α∂each
␈↓ ↓H␈↓␈↓ ¬(half␈α⊂of␈α⊂ac␈α∂are␈α⊂then␈α⊂decreased␈α∂by␈α⊂1.␈α⊂ This␈α⊂may␈α∂be
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :40
␈↓ ↓H␈↓␈↓ ¬(used␈α∀for␈α∪removing␈α∀the␈α∀top␈α∪element␈α∀of␈α∀the␈α∪stack.
␈↓ ↓H␈↓␈↓ ¬(Thus␈α
(POP␈α
P␈α1)␈α
puts␈α
the␈α
top␈αelement␈α
of␈α
the␈αstack␈α
in
␈↓ ↓H␈↓␈↓ ¬(accumulator␈α∪1.␈α∪ The␈α∪i-th␈α∪element␈α∪of␈α∪the␈α∪stack␈α∩is
␈↓ ↓H␈↓␈↓ ¬(obtained by (MOVE@ 1 i P).
␈↓ ↓H␈↓␈↓ αHPOPJ ␈↓ ¬((POPJ P) is used for returning from a subroutine
␈↓ ↓H␈↓ These␈α∂instructions␈α⊂are␈α∂adequate␈α∂for␈α⊂compiling␈α∂basic␈α⊂LISP␈α∂code␈α∂with␈α⊂ the␈α∂addition␈α⊂of␈α∂the
␈↓ ↓H␈↓subroutine␈α
calling␈α
pseudo-instrucion.␈α
(CALL␈α␈↓αn␈↓␈α
(E␈α
<subr)␈α
S)␈α
is␈αused␈α
for␈α
calling␈α
the␈αLISP␈α
subroutine
␈↓ ↓H␈↓<subr>␈α∞with␈α∞ ␈↓αn␈↓␈α∂arguments.␈α∞ The␈α∞ convention␈α∂ is␈α∞that␈α∞the␈α∞arguments␈α∂will␈α∞be␈α∞stored␈α∂in␈α∞successive
␈↓ ↓H␈↓accumulators␈αbeginning␈αwith␈αaccumulator␈α 1,␈αand␈αthe␈αresult␈αwill␈α be␈α returned␈α in␈α accumulator␈α 1.
␈↓ ↓H␈↓In␈α
particular␈αthe␈α
functions␈α
ATOM ␈αand␈α
CONS ␈αare␈α
called␈α
with␈α (CALL␈α
1␈α(E␈α
ATOM)␈α
S) ␈αand
␈↓ ↓H␈↓(CALL␈α∂2␈α∂ (E␈α∂CONS)␈α⊂S)␈α∂ respectively,␈α∂ and␈α∂ programs␈α∂produced␈α⊂by␈α∂you␈α∂compiler␈α∂will␈α⊂be␈α∂called
␈↓ ↓H␈↓similarly when their names are referred to.
␈↓ ↓H␈↓3. ␈↓βCode produced by LISP compilers.␈↓
␈↓ ↓H␈↓ We␈αwill␈αdiscuss␈αtwo␈αcompilers,␈αa␈αsimple␈αone␈αcalled␈αLCOM0␈αand␈αa␈αmore␈αoptimising␈αcompiler
␈↓ ↓H␈↓called␈α
LCOM4.␈α∞ LCOM4␈α
produces␈α
about␈α∞half␈α
as␈α
many␈α∞instructions␈α
for␈α
a␈α∞given␈α
function␈α∞as␈α
does
␈↓ ↓H␈↓LCOM0.␈αBesides␈α
these,␈αthere␈α
is␈αthe␈αstandard␈α
PDP-10␈αLISP␈α
compiler␈αwritten␈α
at␈αM.I.T.␈α by␈α
Richard
␈↓ ↓H␈↓Greenblatt and Stuart Nelson and modified by Whitfield Diffie.
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :41
␈↓ ↓H␈↓Examples of output of LCOM0, LCOM4, and the regular Lisp compiler
␈↓ ↓H␈↓For the file containing:
␈↓ ↓H␈↓(DEFPROP DROP
␈↓ ↓H␈↓ (LAMBDA(X)
␈↓ ↓H␈↓ (COND ((NULL X) NIL) (T (CONS (LIST (CAR X)) (DROP (CDR X))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓******************************************************************
␈↓ ↓H␈↓LCOM0 produces the following code:
␈↓ ↓H␈↓(LAP DROP SUBR)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(CALL 1 (E NULL) S)
␈↓ ↓H␈↓(JUMPE 1 G0163)
␈↓ ↓H␈↓(MOVEI 1 0)
␈↓ ↓H␈↓(JRST G0162)
␈↓ ↓H␈↓G0163
␈↓ ↓H␈↓(MOVEI 1 (QUOTE T))
␈↓ ↓H␈↓(JUMPE 1 G0164)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(CALL 1 (E CAR) S)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(CALL 1 (E LIST) S)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 -1 P)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(CALL 1 (E CDR) S)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(CALL 1 (E DROP) S)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 -1 P)
␈↓ ↓H␈↓(MOVE 2 0 P)
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :42
␈↓ ↓H␈↓(SUB P (C 2 0 2 0))
␈↓ ↓H␈↓(CALL 2 (E CONS) S)
␈↓ ↓H␈↓(JRST G0162)
␈↓ ↓H␈↓G0164
␈↓ ↓H␈↓G0162
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(POPJ P)
␈↓ ↓H␈↓NIL
␈↓ ↓H␈↓LCOM4 produces the following code:
␈↓ ↓H␈↓(LAP DROP SUBR)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(JUMPE 1 G0162)
␈↓ ↓H␈↓(HLRZ@ 1 0 P)
␈↓ ↓H␈↓(CALL 1 (E LIST) S)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(HRRZ@ 1 -1 P)
␈↓ ↓H␈↓(CALL 1 (E DROP) S)
␈↓ ↓H␈↓(MOVE 2 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(CALL 2 (E CONS) S)
␈↓ ↓H␈↓G0162
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(POPJ P)
␈↓ ↓H␈↓NIL
␈↓ ↓H␈↓******************************************************************
␈↓ ↓H␈↓The ILISP compiler produces the following code:
␈↓ ↓H␈↓(LAP DROP SUBR)
␈↓ ↓H␈↓ (PUSH P 1)
␈↓ ↓H␈↓ (JUMPE 1 TAG1)
␈↓ ↓H␈↓ (HLRZ@ 1 0 P)
␈↓ ↓H␈↓ (CALL 1 (E NCONS) S)
␈↓ ↓H␈↓ (PUSH P 1)
␈↓ ↓H␈↓ (HRRZ@ 1 -1 P)
␈↓ ↓H␈↓ (CALL 1 (E DROP) S)
␈↓ ↓H␈↓ (POP P 2)
␈↓ ↓H␈↓ (CALL 2 (E XCONS) S)
␈↓ ↓H␈↓ TAG1 (SUB P (C 1 0 1 0))
␈↓ ↓H␈↓ (POPJ P)
␈↓ ↓H␈↓ NIL
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :43
␈↓ ↓H␈↓ Note␈α
that␈αall␈α
three␈α
compilers␈αproduce␈α
the␈αsame␈α
first␈α
line␈αof␈α
heading.␈α This␈α
is␈α
necessary␈αfor
␈↓ ↓H␈↓the␈α⊃LAP␈α⊂assembly␈α⊃program␈α⊂ which␈α⊃ also␈α⊃requires␈α⊂ the␈α⊃ NIL ␈α⊂at␈α⊃the␈α⊂end␈α⊃as␈α⊃punctuation.␈α⊂ The
␈↓ ↓H␈↓standard␈αcompiler␈αuses␈αa␈αfunction␈αcalled␈α XCONS ␈αthat␈αhas␈α the␈α same␈α effect␈α as␈α
CONS ␈αexcept
␈↓ ↓H␈↓that␈α
it␈α
receives␈α
its␈α
arguments␈α
in␈α
the␈α
reverse␈α
order␈α
which␈α
is␈α
sometimes␈α
convenient.␈α
This␈α
requires,␈α
of
␈↓ ↓H␈↓course,␈α
that␈α
the␈αcompiler␈α
be␈α
smart␈α enough␈α
to␈α
decide␈α where␈α
it␈α
is␈α more␈α
convenient␈α
to␈α
put␈αthe
␈↓ ↓H␈↓arguments of the ␈↓αcons␈↓ function.
␈↓ ↓H␈↓ My␈α two␈α compilers␈α are␈α similar␈αin␈αstructure,␈αbut␈αthe␈αbetter␈αone,␈αwhich␈αis␈αtwice␈αas␈αlong,␈αuses
␈↓ ↓H␈↓the following optimisations.
␈↓ ↓H␈↓ 1.␈α∞When␈α∞the␈α∂argument␈α∞of␈α∞CAR␈α∞or␈α∂CDR␈α∞is␈α∞a␈α∂variable,␈α∞it␈α∞compiles␈α∞a␈α∂ (HLRZ@␈α∞ 1␈α∞ i␈α∂P)␈α∞or
␈↓ ↓H␈↓(HRRZ@␈α
1␈αi␈α
P)␈αwhich␈α
gets␈αthe␈α
result␈αthrough␈α
the␈αstack␈α
without␈αfirst␈α
compiling␈αthe␈α
argument␈αinto
␈↓ ↓H␈↓an accumulator.
␈↓ ↓H␈↓ 2.␈αWhen␈αit␈αhas␈α
to␈αset␈αup␈αthe␈α
arguments␈αof␈αa␈αfunction␈α
in␈α the␈αaccumulators,␈α in␈α
general,␈αthe
␈↓ ↓H␈↓program␈αmust␈αcompute␈αthe␈αarguments␈αone␈α
at␈αa␈αtime␈αand␈αsave␈αthem␈α
on␈αthe␈αstack,␈αand␈αthen␈αload␈α
the
␈↓ ↓H␈↓accumulators␈α∪from␈α∀the␈α∪stack.␈α∀ However,␈α∪if␈α∀one␈α∪of␈α∀the␈α∪arguments␈α∀is␈α∪a␈α∀variable,␈α∪is␈α∀a␈α∪quoted
␈↓ ↓H␈↓expression,␈α
or␈α
can␈α
be␈α
obtained␈αfrom␈α
a␈α
variable␈α
by␈α
a␈α
chain␈α of␈α
cars ␈α
and␈α
cdrs,␈α
then␈α
it␈α need␈α
not
␈↓ ↓H␈↓be␈αcomputed␈αuntil␈αthe␈αtime␈αof␈αloading␈α accumulators␈α since␈α it␈α can␈α be␈α computed␈α using␈α only␈α the
␈↓ ↓H␈↓accumulator in which it is wanted.
␈↓ ↓H␈↓ 3.␈α∩ The␈α∩ Diffy␈α∩ compiler␈α⊃ produces␈α∩about␈α∩10␈α∩percent␈α⊃less␈α∩code␈α∩than␈α∩LCOM4;␈α∩the␈α⊃main
␈↓ ↓H␈↓difference␈αseems␈αto␈α
be␈αthat␈αit␈α
sometimes␈αnotices␈αwhen␈α it␈α
has␈α an␈α expression␈α
that␈αit␈αwill␈αneed␈α
later.
␈↓ ↓H␈↓Sometimes␈α
this␈αfeature␈α
leads␈α
it␈αastray.␈α
For␈αexample,␈α
it␈α
may␈αsave␈α
␈↓βa␈α␈↓αu␈↓ ␈α
for␈α
later␈αuse␈α
at␈α
greater␈αcost
␈↓ ↓H␈↓than re-computing it.
␈↓ ↓H␈↓ It should further be noted that quoted S-expressions can be compiled with the instruction
␈↓ ↓H␈↓ (MOVEI 1 (QUOTE α)).
␈↓ ↓H␈↓4. ␈↓βListings of LCOM0 and LCOM4␈↓
␈↓ ↓H␈↓ First␈α∀are␈α∀listings␈α∪of␈α∀both␈α∀compilers␈α∪in␈α∀blackboard␈α∀notation.␈α∪ Following␈α∀these␈α∀is␈α∀an␈α∪an
␈↓ ↓H␈↓annotated listing of LCOM0 in S-expression notation, and a listing of LCOM4 in that notation.
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :44
␈↓ ↓H␈↓LCOM0:
␈↓ ↓H␈↓␈↓ ↓x␈↓αcompl␈↓↓ ␈↓αfile␈↓↓ (␈↓∧FEXPR␈↓↓) ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βprog␈↓↓ [␈↓αz␈↓↓]
␈↓ ↓H␈↓↓␈↓ αa␈↓αeval␈↓↓ ␈↓∧OUTPUT␈↓↓ . [ `␈↓∧DSK:␈↓↓' . <␈↓βa ␈↓αfile␈↓↓ . ␈↓∧LAP␈↓↓>]
␈↓ ↓H␈↓↓␈↓ αa␈↓αeval␈↓↓ ␈↓∧INPUT␈↓↓ . [ `␈↓∧DSK:␈↓↓' . ␈↓αfile␈↓↓]
␈↓ ↓H␈↓↓␈↓ αa␈↓αinc␈↓↓[␈↓∧T␈↓↓, ␈↓∧NIL␈↓↓]
␈↓ ↓H␈↓↓␈↓ αa␈↓αoutc␈↓↓[␈↓∧T␈↓↓, ␈↓∧NIL␈↓↓]
␈↓ ↓H␈↓↓␈↓ α#␈↓αloop␈↓↓ ␈↓αz␈↓↓ ← ␈↓αerrset␈↓↓ ␈↓αread␈↓↓[]
␈↓ ↓H␈↓↓␈↓ αa␈↓βif␈↓↓ ␈↓βat ␈↓αz␈↓↓ ␈↓βthen␈↓↓ ␈↓αgo␈↓↓ ␈↓αdone␈↓↓ ␈↓βelse if␈↓↓ ␈↓∧T␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL
␈↓ ↓H␈↓∧␈↓ αa␈↓αz␈↓↓ ← ␈↓βa ␈↓αz
␈↓ ↓H␈↓α␈↓ αa␈↓βif␈↓↓ ␈↓βa ␈↓αz␈↓↓ ␈↓βeq ␈↓∧DE␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ β↓[␈↓βprog␈↓↓ [␈↓αprog␈↓↓]
␈↓ ↓H␈↓↓␈↓ βS␈↓αprog␈↓↓ ← ␈↓αcomp␈↓↓[␈↓βad ␈↓αz␈↓↓, ␈↓βadd ␈↓αz␈↓↓, ␈↓βaddd ␈↓αz␈↓↓]
␈↓ ↓H␈↓↓␈↓ βS␈↓αmapc␈↓↓[␈↓αprint␈↓↓, ␈↓αprog␈↓↓]
␈↓ ↓H␈↓↓␈↓ βS␈↓αoutc␈↓↓[␈↓∧NIL␈↓↓, ␈↓∧NIL␈↓↓]
␈↓ ↓H␈↓↓␈↓ βS␈↓αprint␈↓↓ <␈↓βad ␈↓αz␈↓↓, ␈↓αlength␈↓↓ ␈↓αprog␈↓↓>
␈↓ ↓H␈↓↓␈↓ βS␈↓αoutc␈↓↓[␈↓∧T␈↓↓, ␈↓∧NIL␈↓↓]]
␈↓ ↓H␈↓↓␈↓ αq␈↓βelse␈↓↓ ␈↓αprint␈↓↓ ␈↓αz
␈↓ ↓H␈↓α␈↓ αago␈↓↓ ␈↓αloop
␈↓ ↓H␈↓α␈↓ α≥done␈↓↓ ␈↓αoutc␈↓↓[␈↓∧NIL␈↓↓, ␈↓∧T␈↓↓]
␈↓ ↓H␈↓↓␈↓ αa␈↓αinc␈↓↓[␈↓∧NIL␈↓↓, ␈↓∧T␈↓↓]
␈↓ ↓H␈↓↓␈↓ αa␈↓αreturn␈↓↓ ␈↓∧ENDCOMP
␈↓ ↓H␈↓∧␈↓ ↓x␈↓αcomp␈↓↓[␈↓αfn␈↓↓, ␈↓αvars␈↓↓, ␈↓αexp␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_{␈↓αlength␈↓↓ ␈↓αvars␈↓↓}[λ␈↓αn␈↓↓.
␈↓ ↓H␈↓↓␈↓ α8<<␈↓∧LAP␈↓↓, ␈↓αfn␈↓↓, ␈↓∧SUBR␈↓↓>>
␈↓ ↓H␈↓↓␈↓ α8* ␈↓αmkpush␈↓↓[␈↓αn␈↓↓, ␈↓∧1␈↓↓]
␈↓ ↓H␈↓↓␈↓ α8* ␈↓αcompexp␈↓↓[␈↓αexp␈↓↓, -␈↓αn␈↓↓, ␈↓αprup␈↓↓[␈↓αvars␈↓↓, ␈↓∧1␈↓↓]]
␈↓ ↓H␈↓↓␈↓ α8* <<␈↓∧SUB␈↓↓, ␈↓∧P␈↓↓, <␈↓∧C␈↓↓, ␈↓αn␈↓↓, ␈↓∧0␈↓↓, ␈↓αn␈↓↓, ␈↓∧0␈↓↓>>>
␈↓ ↓H␈↓↓␈↓ α8* ((␈↓∧POPJ␈↓↓ ␈↓∧P␈↓↓) ␈↓∧NIL␈↓↓)]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αprup␈↓↓[␈↓αvars␈↓↓, ␈↓αn␈↓↓] ← ␈↓βif␈↓↓ ␈↓βn ␈↓αvars␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL␈↓↓ ␈↓βelse␈↓↓ [␈↓βa ␈↓αvars␈↓↓ . ␈↓αn␈↓↓] . ␈↓αprup␈↓↓[␈↓βd ␈↓αvars␈↓↓, ␈↓αn␈↓↓ + ␈↓∧1␈↓↓]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αmkpush␈↓↓[␈↓αn␈↓↓, ␈↓αm␈↓↓] ← ␈↓βif␈↓↓ ␈↓αn␈↓↓ < ␈↓αm␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL␈↓↓ ␈↓βelse␈↓↓ <␈↓∧PUSH␈↓↓, ␈↓∧P␈↓↓, ␈↓αm␈↓↓> . ␈↓αmkpush␈↓↓[␈↓αn␈↓↓, ␈↓αm␈↓↓ + ␈↓∧1␈↓↓]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αcompexp␈↓↓[␈↓αexp␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βn ␈↓αexp␈↓↓ ␈↓βthen␈↓↓ ((␈↓∧MOVEI␈↓↓ ␈↓∧1␈↓↓ ␈↓∧0␈↓↓))
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :45
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧T␈↓↓ ␈↓βthen␈↓↓ ((␈↓∧MOVEI␈↓↓ ␈↓∧1␈↓↓ (␈↓∧QUOTE␈↓↓ ␈↓∧T␈↓↓)))
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βat ␈↓αexp␈↓↓ ␈↓βthen␈↓↓ <<␈↓∧MOVE␈↓↓, ␈↓∧1␈↓↓, ␈↓αm␈↓↓ + ␈↓βd ␈↓αassoc␈↓↓[␈↓αexp␈↓↓, ␈↓αvpr␈↓↓], ␈↓∧P␈↓↓>>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧AND␈↓↓ ∨ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧OR␈↓↓ ∨ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧NOT␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8{␈↓αgensym␈↓↓[], ␈↓αgensym␈↓↓[]}[λ␈↓αl1␈↓↓, ␈↓αl2␈↓↓.
␈↓ ↓H␈↓↓␈↓ αX␈↓αcombool␈↓↓[␈↓αexp␈↓↓, ␈↓αm␈↓↓, ␈↓αl1␈↓↓, ␈↓∧NIL␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ αX* <(␈↓∧MOVEI␈↓↓ ␈↓∧1␈↓↓ (␈↓∧QUOTE␈↓↓ ␈↓∧T␈↓↓)), <␈↓∧JRST␈↓↓, ␈↓∧0␈↓↓, ␈↓αl2␈↓↓>, ␈↓αl1␈↓↓, (␈↓∧MOVEI␈↓↓ ␈↓∧1␈↓↓ ␈↓∧0␈↓↓), ␈↓αl2␈↓↓>]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧COND␈↓↓ ␈↓βthen␈↓↓ ␈↓αcomcond␈↓↓[␈↓βd ␈↓αexp␈↓↓, ␈↓αm␈↓↓, ␈↓αgensym␈↓↓[], ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧QUOTE␈↓↓ ␈↓βthen␈↓↓ <<␈↓∧MOVEI␈↓↓, ␈↓∧1␈↓↓, ␈↓αexp␈↓↓>>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βat a ␈↓αexp␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8{␈↓αlength␈↓↓ ␈↓βd ␈↓αexp␈↓↓}[λ␈↓αn␈↓↓.
␈↓ ↓H␈↓↓␈↓ αX␈↓αcomplis␈↓↓[␈↓βd ␈↓αexp␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ αX* ␈↓αloadac␈↓↓[␈↓∧1␈↓↓ - ␈↓αn␈↓↓, ␈↓∧1␈↓↓]
␈↓ ↓H␈↓↓␈↓ αX* <<␈↓∧SUB␈↓↓, ␈↓∧P␈↓↓, <␈↓∧C␈↓↓, ␈↓αn␈↓↓, ␈↓∧0␈↓↓, ␈↓αn␈↓↓, ␈↓∧0␈↓↓>>>
␈↓ ↓H␈↓↓␈↓ αX* <<␈↓∧CALL␈↓↓, ␈↓αn␈↓↓, <␈↓∧E␈↓↓, ␈↓βa ␈↓αexp␈↓↓>, ␈↓∧S␈↓↓>>]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βaa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧LAMBDA␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8{␈↓αlength␈↓↓ ␈↓βd ␈↓αexp␈↓↓}[λ␈↓αn␈↓↓.
␈↓ ↓H␈↓↓␈↓ αX␈↓αcomplis␈↓↓[␈↓βd ␈↓αexp␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ αX* ␈↓αcompexp␈↓↓[␈↓βadda ␈↓αexp␈↓↓, ␈↓αm␈↓↓ - ␈↓αn␈↓↓, ␈↓αprup␈↓↓[␈↓βada ␈↓αexp␈↓↓, ␈↓∧1␈↓↓ - ␈↓αm␈↓↓] * ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ αX* <<␈↓∧SUB␈↓↓, ␈↓∧P␈↓↓, <␈↓∧C␈↓↓, ␈↓αn␈↓↓, ␈↓∧0␈↓↓, ␈↓αn␈↓↓, ␈↓∧0␈↓↓>>>]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓∧T␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL
␈↓ ↓H␈↓∧␈↓ ↓x␈↓αcomplis␈↓↓[␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βn ␈↓αu␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL␈↓↓ ␈↓βelse␈↓↓ ␈↓αcompexp␈↓↓[␈↓βa ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓] * ((␈↓∧PUSH␈↓↓ ␈↓∧P␈↓↓ ␈↓∧1␈↓↓)) * ␈↓αcomplis␈↓↓[␈↓βd ␈↓αu␈↓↓, ␈↓αm␈↓↓ - ␈↓∧1␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αloadac␈↓↓[␈↓αn␈↓↓, ␈↓αk␈↓↓] ← ␈↓βif␈↓↓ ␈↓αn␈↓↓ > ␈↓∧0␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL␈↓↓ ␈↓βelse␈↓↓ <␈↓∧MOVE␈↓↓, ␈↓αk␈↓↓, ␈↓αn␈↓↓, ␈↓∧P␈↓↓> . ␈↓αloadac␈↓↓[␈↓αn␈↓↓ + ␈↓∧1␈↓↓, ␈↓αk␈↓↓ + ␈↓∧1␈↓↓]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αcomcond␈↓↓[␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βn ␈↓αu␈↓↓ ␈↓βthen␈↓↓ <␈↓αl␈↓↓>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse␈↓↓ {␈↓αgensym␈↓↓[]}[λ␈↓αl1␈↓↓.
␈↓ ↓H␈↓↓␈↓ α8␈↓αcombool␈↓↓[␈↓βaa ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl1␈↓↓, ␈↓∧NIL␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ α8* ␈↓αcompexp␈↓↓[␈↓βada ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ α8* <<␈↓∧JRST␈↓↓, ␈↓αl␈↓↓>, ␈↓αl1␈↓↓>
␈↓ ↓H␈↓↓␈↓ α8* ␈↓αcomcond␈↓↓[␈↓βd ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αvpr␈↓↓]]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αcombool␈↓↓[␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αflg␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βat ␈↓αp␈↓↓ ␈↓βthen␈↓↓ [␈↓αcompexp␈↓↓[␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓] * <<␈↓βif␈↓↓ ␈↓αflg␈↓↓ ␈↓βthen␈↓↓ ␈↓∧JUMPN␈↓↓ ␈↓βelse␈↓↓ ␈↓∧JUMPE␈↓↓, ␈↓∧1␈↓↓, ␈↓αl␈↓↓>>]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αp␈↓↓ ␈↓βeq ␈↓∧AND␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8[␈↓βif␈↓↓ ¬␈↓αflg␈↓↓ ␈↓βthen␈↓↓ ␈↓αcompandor␈↓↓[␈↓βd ␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓∧NIL␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :46
␈↓ ↓H␈↓↓␈↓ αA␈↓βelse␈↓↓ {␈↓αgensym␈↓↓[]}[λ␈↓αl1␈↓↓. ␈↓αcompandor␈↓↓[␈↓βd ␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αl1␈↓↓, ␈↓∧NIL␈↓↓, ␈↓αvpr␈↓↓] * <<␈↓∧JRST␈↓↓, ␈↓∧0␈↓↓, ␈↓αl␈↓↓>> * <␈↓αl1␈↓↓>]]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αp␈↓↓ ␈↓βeq ␈↓∧OR␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8[␈↓βif␈↓↓ ␈↓αflg␈↓↓ ␈↓βthen␈↓↓ ␈↓αcompandor␈↓↓[␈↓βd ␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓∧T␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ αA␈↓βelse␈↓↓ {␈↓αgensym␈↓↓[]}[λ␈↓αl1␈↓↓. ␈↓αcompandor␈↓↓[␈↓βd ␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αl1␈↓↓, ␈↓∧T␈↓↓, ␈↓αvpr␈↓↓] * <<␈↓∧JRST␈↓↓, ␈↓∧0␈↓↓, ␈↓αl␈↓↓>> * <␈↓αl1␈↓↓>]]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αp␈↓↓ ␈↓βeq ␈↓∧NOT␈↓↓ ␈↓βthen␈↓↓ ␈↓αcombool␈↓↓[␈↓βad ␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ¬␈↓αflg␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse␈↓↓ ␈↓αcompexp␈↓↓[␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓] * <<␈↓βif␈↓↓ ␈↓αflg␈↓↓ ␈↓βthen␈↓↓ ␈↓∧JUMPN␈↓↓ ␈↓βelse␈↓↓ ␈↓∧JUMPE␈↓↓, ␈↓∧1␈↓↓, ␈↓αl␈↓↓>>
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αcompandor␈↓↓[␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αflg␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βn ␈↓αu␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL␈↓↓ ␈↓βelse␈↓↓ ␈↓αcombool␈↓↓[␈↓βa ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αflg␈↓↓, ␈↓αvpr␈↓↓] * ␈↓αcompandor␈↓↓[␈↓βd ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αflg␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :47
␈↓ ↓H␈↓LCOM4:
␈↓ ↓H␈↓␈↓ ↓x␈↓αcompl␈↓↓ ␈↓αfile␈↓↓ (␈↓∧FEXPR␈↓↓) ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βprog␈↓↓ [␈↓αz␈↓↓]
␈↓ ↓H␈↓↓␈↓ αa␈↓αeval␈↓↓ ␈↓∧OUTPUT␈↓↓ . [ `␈↓∧DSK:␈↓↓' . <␈↓βa ␈↓αfile␈↓↓ . ␈↓∧LAP␈↓↓>]
␈↓ ↓H␈↓↓␈↓ αa␈↓αeval␈↓↓ ␈↓∧INPUT␈↓↓ . [ `␈↓∧DSK:␈↓↓' . ␈↓αfile␈↓↓]
␈↓ ↓H␈↓↓␈↓ αa␈↓αinc␈↓↓[␈↓∧T␈↓↓, ␈↓∧NIL␈↓↓]
␈↓ ↓H␈↓↓␈↓ αa␈↓αoutc␈↓↓[␈↓∧T␈↓↓, ␈↓∧NIL␈↓↓]
␈↓ ↓H␈↓↓␈↓ α#␈↓αloop␈↓↓ ␈↓αz␈↓↓ ← ␈↓αerrset␈↓↓ ␈↓αread␈↓↓[]
␈↓ ↓H␈↓↓␈↓ αa␈↓βif␈↓↓ ␈↓βat ␈↓αz␈↓↓ ␈↓βthen␈↓↓ ␈↓αgo␈↓↓ ␈↓αdone␈↓↓ ␈↓βelse if␈↓↓ ␈↓∧T␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL
␈↓ ↓H␈↓∧␈↓ αa␈↓αz␈↓↓ ← ␈↓βa ␈↓αz
␈↓ ↓H␈↓α␈↓ αa␈↓βif␈↓↓ ␈↓βa ␈↓αz␈↓↓ ␈↓βeq ␈↓∧DE␈↓↓ ∨ [␈↓βa ␈↓αz␈↓↓ ␈↓βeq ␈↓∧DEFPROP␈↓↓ ∧ ␈↓βaddd ␈↓αz␈↓↓ ␈↓βeq ␈↓∧EXPR␈↓↓] ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ β↓[␈↓βprog␈↓↓ [␈↓αprog␈↓↓]
␈↓ ↓H␈↓↓␈↓ βS␈↓αprog
␈↓ ↓H␈↓α␈↓ βc␈↓↓← [␈↓βif␈↓↓ ␈↓βa ␈↓αz␈↓↓ ␈↓βeq ␈↓∧DE␈↓↓ ␈↓βthen␈↓↓ ␈↓αcomp␈↓↓[␈↓βad ␈↓αz␈↓↓, ␈↓βadd ␈↓αz␈↓↓, ␈↓βaddd ␈↓αz␈↓↓]
␈↓ ↓H␈↓↓␈↓ ∧ ␈↓βelse␈↓↓ ␈↓αcomp␈↓↓[␈↓βad ␈↓αz␈↓↓, ␈↓βad add ␈↓αz␈↓↓, ␈↓βadd add ␈↓αz␈↓↓]]
␈↓ ↓H␈↓↓␈↓ βS␈↓αmapc␈↓↓[␈↓αprint␈↓↓, ␈↓αprog␈↓↓]
␈↓ ↓H␈↓↓␈↓ βS␈↓αoutc␈↓↓[␈↓∧NIL␈↓↓, ␈↓∧NIL␈↓↓]
␈↓ ↓H␈↓↓␈↓ βS␈↓αprint␈↓↓ <␈↓βad ␈↓αz␈↓↓, ␈↓αlength␈↓↓ ␈↓αprog␈↓↓>
␈↓ ↓H␈↓↓␈↓ βS␈↓αoutc␈↓↓[␈↓∧T␈↓↓, ␈↓∧NIL␈↓↓]]
␈↓ ↓H␈↓↓␈↓ αq␈↓βelse␈↓↓ ␈↓αprint␈↓↓ ␈↓αz
␈↓ ↓H␈↓α␈↓ αago␈↓↓ ␈↓αloop
␈↓ ↓H␈↓α␈↓ α≥done␈↓↓ ␈↓αoutc␈↓↓[␈↓∧NIL␈↓↓, ␈↓∧T␈↓↓]
␈↓ ↓H␈↓↓␈↓ αa␈↓αinc␈↓↓[␈↓∧NIL␈↓↓, ␈↓∧T␈↓↓]
␈↓ ↓H␈↓↓␈↓ αa␈↓αreturn␈↓↓ ␈↓∧ENDCOMP
␈↓ ↓H␈↓∧␈↓ ↓x␈↓αcomp␈↓↓[␈↓αfn␈↓↓, ␈↓αvars␈↓↓, ␈↓αexp␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_{␈↓αprup␈↓↓[␈↓αvars␈↓↓, ␈↓∧1␈↓↓], ␈↓αlength␈↓↓ ␈↓αvars␈↓↓}[λ␈↓αvpr␈↓↓, ␈↓αn␈↓↓.
␈↓ ↓H␈↓↓␈↓ α8␈↓αflat␈↓↓[<<<␈↓∧LAP␈↓↓, ␈↓αfn␈↓↓, ␈↓∧SUBR␈↓↓>>,
␈↓ ↓H␈↓↓␈↓ β↓␈↓αmkpush␈↓↓[␈↓αn␈↓↓, ␈↓∧1␈↓↓],
␈↓ ↓H␈↓↓␈↓ β↓␈↓αcompexp␈↓↓[␈↓αexp␈↓↓, -␈↓αn␈↓↓, ␈↓αvpr␈↓↓],
␈↓ ↓H␈↓↓␈↓ β↓␈↓αsubstack␈↓↓ ␈↓αn␈↓↓,
␈↓ ↓H␈↓↓␈↓ β↓((␈↓∧POPJ␈↓↓ ␈↓∧P␈↓↓) (␈↓∧LABEL␈↓↓ ␈↓∧NIL␈↓↓))>,
␈↓ ↓H␈↓↓␈↓ αo␈↓∧NIL␈↓↓]]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αsubstack␈↓↓ ␈↓αn␈↓↓ ← ␈↓βif␈↓↓ ␈↓αn␈↓↓ ␈↓βeq ␈↓∧0␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL␈↓↓ ␈↓βelse␈↓↓ <<␈↓∧SUB␈↓↓, ␈↓∧P␈↓↓, <␈↓∧C␈↓↓, ␈↓αn␈↓↓, ␈↓∧0␈↓↓, ␈↓αn␈↓↓, ␈↓∧0␈↓↓>>>
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αprup␈↓↓[␈↓αvars␈↓↓, ␈↓αn␈↓↓] ← ␈↓βif␈↓↓ ␈↓βn ␈↓αvars␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL␈↓↓ ␈↓βelse␈↓↓ [␈↓βa ␈↓αvars␈↓↓ . ␈↓αn␈↓↓] . ␈↓αprup␈↓↓[␈↓βd ␈↓αvars␈↓↓, ␈↓αn␈↓↓ + ␈↓∧1␈↓↓]
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :48
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αmkpush␈↓↓[␈↓αn␈↓↓, ␈↓αm␈↓↓] ← ␈↓βif␈↓↓ ␈↓αn␈↓↓ < ␈↓αm␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL␈↓↓ ␈↓βelse␈↓↓ <␈↓∧PUSH␈↓↓, ␈↓∧P␈↓↓, ␈↓αm␈↓↓> . ␈↓αmkpush␈↓↓[␈↓αn␈↓↓, ␈↓αm␈↓↓ + ␈↓∧1␈↓↓]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αcompexp␈↓↓[␈↓αexp␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βn ␈↓αexp␈↓↓ ␈↓βthen␈↓↓ ((␈↓∧MOVEI␈↓↓ ␈↓∧1␈↓↓ ␈↓∧0␈↓↓))
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧T␈↓↓ ∨ ␈↓αnumberp␈↓↓ ␈↓αexp␈↓↓ ␈↓βthen␈↓↓ <<␈↓∧MOVEI␈↓↓, ␈↓∧1␈↓↓, <␈↓∧QUOTE␈↓↓, ␈↓αexp␈↓↓>>>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βat ␈↓αexp␈↓↓ ␈↓βthen␈↓↓ <<␈↓∧MOVE␈↓↓, ␈↓∧1␈↓↓, ␈↓αm␈↓↓ + ␈↓βd ␈↓αassoc␈↓↓[␈↓αexp␈↓↓, ␈↓αvpr␈↓↓], ␈↓∧P␈↓↓>>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧CAR␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8[␈↓βif␈↓↓ ␈↓βat ad ␈↓αexp␈↓↓ ␈↓βthen␈↓↓ << `␈↓∧HLRZ@␈↓↓' , ␈↓∧1␈↓↓, ␈↓αm␈↓↓ + ␈↓βd ␈↓αassoc␈↓↓[␈↓βad ␈↓αexp␈↓↓, ␈↓αvpr␈↓↓], ␈↓∧P␈↓↓>>
␈↓ ↓H␈↓↓␈↓ αA␈↓βelse␈↓↓ <␈↓αcompexp␈↓↓[␈↓βad ␈↓αexp␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓], (( `␈↓∧HLRZ@␈↓↓' ␈↓∧1␈↓↓ ␈↓∧1␈↓↓))>]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧CDR␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8[␈↓βif␈↓↓ ␈↓βat ad ␈↓αexp␈↓↓ ␈↓βthen␈↓↓ << `␈↓∧HRRZ@␈↓↓' , ␈↓∧1␈↓↓, ␈↓αm␈↓↓ + ␈↓βd ␈↓αassoc␈↓↓[␈↓βad ␈↓αexp␈↓↓, ␈↓αvpr␈↓↓], ␈↓∧P␈↓↓>>
␈↓ ↓H␈↓↓␈↓ αA␈↓βelse␈↓↓ <␈↓αcompexp␈↓↓[␈↓βad ␈↓αexp␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓], (( `␈↓∧HRRZ@␈↓↓' ␈↓∧1␈↓↓ ␈↓∧1␈↓↓))>]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧AND␈↓↓ ∨ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧OR␈↓↓ ∨ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧NOT␈↓↓ ∨ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧EQ␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8{␈↓αgensym␈↓↓[], ␈↓αgensym␈↓↓[]}[λ␈↓αl1␈↓↓, ␈↓αl2␈↓↓.
␈↓ ↓H␈↓↓␈↓ αX<␈↓αcombool␈↓↓[␈↓αexp␈↓↓, ␈↓αm␈↓↓, ␈↓αl1␈↓↓, ␈↓∧NIL␈↓↓, ␈↓αvpr␈↓↓],
␈↓ ↓H␈↓↓␈↓ αj<(␈↓∧MOVEI␈↓↓ ␈↓∧1␈↓↓ (␈↓∧QUOTE␈↓↓ ␈↓∧T␈↓↓)), <␈↓∧JRST␈↓↓, ␈↓∧0␈↓↓, ␈↓αl2␈↓↓>, <␈↓∧LABEL␈↓↓, ␈↓αl1␈↓↓>, (␈↓∧MOVEI␈↓↓ ␈↓∧1␈↓↓ ␈↓∧0␈↓↓), <␈↓∧LABEL␈↓↓, ␈↓αl2␈↓↓>>>]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧COND␈↓↓ ␈↓βthen␈↓↓ ␈↓αcomcond␈↓↓[␈↓βd ␈↓αexp␈↓↓, ␈↓αm␈↓↓, ␈↓αgensym␈↓↓[], ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧QUOTE␈↓↓ ␈↓βthen␈↓↓ <<␈↓∧MOVEI␈↓↓, ␈↓∧1␈↓↓, ␈↓αexp␈↓↓>>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βat a ␈↓αexp␈↓↓ ␈↓βthen␈↓↓ <␈↓αcomplisa␈↓↓[␈↓βd ␈↓αexp␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓], <<␈↓∧CALL␈↓↓, ␈↓αlength␈↓↓ ␈↓βd ␈↓αexp␈↓↓, <␈↓∧E␈↓↓, ␈↓βa ␈↓αexp␈↓↓>, ␈↓∧S␈↓↓>>>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βaa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧LAMBDA␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8{␈↓αlength␈↓↓ ␈↓βd ␈↓αexp␈↓↓}[λ␈↓αn␈↓↓.
␈↓ ↓H␈↓↓␈↓ αX<␈↓αstackup␈↓↓[␈↓βd ␈↓αexp␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓],
␈↓ ↓H␈↓↓␈↓ αj␈↓αcompexp␈↓↓[␈↓βadda ␈↓αexp␈↓↓, ␈↓αm␈↓↓ - ␈↓αn␈↓↓, ␈↓αapend␈↓↓[␈↓αprup␈↓↓[␈↓βada ␈↓αexp␈↓↓, ␈↓∧1␈↓↓ - ␈↓αm␈↓↓], ␈↓αvpr␈↓↓]],
␈↓ ↓H␈↓↓␈↓ αj␈↓αsubstack␈↓↓ ␈↓αn␈↓↓>]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓∧T␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL
␈↓ ↓H␈↓∧␈↓ ↓x␈↓αstackup␈↓↓[␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βn ␈↓αu␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL␈↓↓ ␈↓βelse␈↓↓ <␈↓αcompexp␈↓↓[␈↓βa ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓], ((␈↓∧PUSH␈↓↓ ␈↓∧P␈↓↓ ␈↓∧1␈↓↓)), ␈↓αstackup␈↓↓[␈↓βd ␈↓αu␈↓↓, ␈↓αm␈↓↓ - ␈↓∧1␈↓↓, ␈↓αvpr␈↓↓]>
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αccchain␈↓↓ ␈↓αexp␈↓↓ ← [␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧CAR␈↓↓ ∨ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧CDR␈↓↓] ∧ [␈↓βat ad ␈↓αexp␈↓↓ ∨ ␈↓αccchain␈↓↓ ␈↓βad ␈↓αexp␈↓↓]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αcompc␈↓↓[␈↓αexp␈↓↓, ␈↓αn2␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βat ␈↓αexp␈↓↓ ␈↓βthen␈↓↓ ␈↓αerr␈↓↓ ␈↓∧COMPC
␈↓ ↓H␈↓∧␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αexp␈↓↓ ␈↓βeq ␈↓∧CAR␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8[␈↓βif␈↓↓ ␈↓βat ad ␈↓αexp␈↓↓ ␈↓βthen␈↓↓ << `␈↓∧HLRZ@␈↓↓' , ␈↓αn2␈↓↓, ␈↓αm␈↓↓ + ␈↓βd ␈↓αassoc␈↓↓[␈↓βad ␈↓αexp␈↓↓, ␈↓αvpr␈↓↓], ␈↓∧P␈↓↓>>
␈↓ ↓H␈↓↓␈↓ αA␈↓βelse␈↓↓ < `␈↓∧HLRZ@␈↓↓' , ␈↓αn2␈↓↓, ␈↓αn2␈↓↓> . ␈↓αcompc␈↓↓[␈↓βad ␈↓αexp␈↓↓, ␈↓αn2␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓]]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βat ad ␈↓αexp␈↓↓ ␈↓βthen␈↓↓ << `␈↓∧HRRZ@␈↓↓' , ␈↓αn2␈↓↓, ␈↓αm␈↓↓ + ␈↓βd ␈↓αassoc␈↓↓[␈↓βad ␈↓αexp␈↓↓, ␈↓αvpr␈↓↓], ␈↓∧P␈↓↓>>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse␈↓↓ < `␈↓∧HRRZ@␈↓↓' , ␈↓αn2␈↓↓, ␈↓αn2␈↓↓> . ␈↓αcompc␈↓↓[␈↓βad ␈↓αexp␈↓↓, ␈↓αn2␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :49
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αcomcond␈↓↓[␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βn ␈↓αu␈↓↓ ␈↓βthen␈↓↓ <<␈↓∧LABEL␈↓↓, ␈↓αl␈↓↓>>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ¬␈↓βat aa ␈↓αu␈↓↓ ∧ ␈↓βaaa ␈↓αu␈↓↓ ␈↓βeq ␈↓∧NULL␈↓↓ ∧ ␈↓βn ada ␈↓αu␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8<␈↓αcompexp␈↓↓[␈↓βadaa ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓], <<␈↓∧JUMPE␈↓↓, ␈↓∧1␈↓↓, ␈↓αl␈↓↓>>, ␈↓αcomcond␈↓↓[␈↓βd ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αvpr␈↓↓]>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βaa ␈↓αu␈↓↓ ␈↓βeq ␈↓∧T␈↓↓ ␈↓βthen␈↓↓ <␈↓αcompexp␈↓↓[␈↓βada ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓], <<␈↓∧LABEL␈↓↓, ␈↓αl␈↓↓>>>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse␈↓↓ {␈↓αgensym␈↓↓[]}[λ␈↓αl1␈↓↓.
␈↓ ↓H␈↓↓␈↓ α8<␈↓αcombool␈↓↓[␈↓βaa ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl1␈↓↓, ␈↓∧NIL␈↓↓, ␈↓αvpr␈↓↓],
␈↓ ↓H␈↓↓␈↓ αJ␈↓αcompexp␈↓↓[␈↓βada ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓],
␈↓ ↓H␈↓↓␈↓ αJ<<␈↓∧JRST␈↓↓, ␈↓∧0␈↓↓, ␈↓αl␈↓↓>, <␈↓∧LABEL␈↓↓, ␈↓αl1␈↓↓>>,
␈↓ ↓H␈↓↓␈↓ αJ␈↓αcomcond␈↓↓[␈↓βd ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αvpr␈↓↓]>]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αcomplisa␈↓↓[␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_{␈↓αclassify␈↓↓ ␈↓αu␈↓↓}[λ␈↓αz␈↓↓.
␈↓ ↓H␈↓↓␈↓ α8<␈↓αcomplis␈↓↓[␈↓αz␈↓↓, ␈↓αm␈↓↓, ␈↓∧1␈↓↓, ␈↓αvpr␈↓↓], ␈↓αloadac␈↓↓[␈↓αz␈↓↓, ␈↓∧1␈↓↓ - ␈↓αccount␈↓↓ ␈↓αz␈↓↓, ␈↓∧1␈↓↓, ␈↓αm␈↓↓ - ␈↓αccount␈↓↓ ␈↓αz␈↓↓, ␈↓αvpr␈↓↓], ␈↓αsubstack␈↓↓ ␈↓αccount␈↓↓ ␈↓αz␈↓↓>]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αccount␈↓↓ ␈↓αz␈↓↓ ← ␈↓βif␈↓↓ ␈↓βn ␈↓αz␈↓↓ ␈↓βthen␈↓↓ ␈↓∧0␈↓↓ ␈↓βelse if␈↓↓ ␈↓βaa ␈↓αz␈↓↓ ␈↓βeq ␈↓∧4␈↓↓ ␈↓βthen␈↓↓ ␈↓∧1␈↓↓ + ␈↓αccount␈↓↓ ␈↓βd ␈↓αz␈↓↓ ␈↓βelse␈↓↓ ␈↓αccount␈↓↓ ␈↓βd ␈↓αz
␈↓ ↓H␈↓α␈↓ ↓xloadac␈↓↓[␈↓αz␈↓↓, ␈↓αm2␈↓↓, ␈↓αn2␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βn ␈↓αz␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL
␈↓ ↓H␈↓∧␈↓ α_␈↓βelse if␈↓↓ ␈↓βaa ␈↓αz␈↓↓ ␈↓βeq ␈↓∧1␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8<␈↓∧MOVE␈↓↓, ␈↓αn2␈↓↓, ␈↓αm␈↓↓ + ␈↓βd ␈↓αassoc␈↓↓[␈↓βda ␈↓αz␈↓↓, ␈↓αvpr␈↓↓], ␈↓∧P␈↓↓> . ␈↓αloadac␈↓↓[␈↓βd ␈↓αz␈↓↓, ␈↓αm2␈↓↓, ␈↓αn2␈↓↓ + ␈↓∧1␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βaa ␈↓αz␈↓↓ ␈↓βeq ␈↓∧0␈↓↓ ␈↓βthen␈↓↓ <␈↓∧MOVEI␈↓↓, ␈↓αn2␈↓↓, <␈↓∧QUOTE␈↓↓, ␈↓βda ␈↓αz␈↓↓>> . ␈↓αloadac␈↓↓[␈↓βd ␈↓αz␈↓↓, ␈↓αm2␈↓↓, ␈↓αn2␈↓↓ + ␈↓∧1␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βaa ␈↓αz␈↓↓ ␈↓βeq ␈↓∧2␈↓↓ ␈↓βthen␈↓↓ <␈↓∧MOVEI␈↓↓, ␈↓αn2␈↓↓, ␈↓βda ␈↓αz␈↓↓> . ␈↓αloadac␈↓↓[␈↓βd ␈↓αz␈↓↓, ␈↓αm2␈↓↓, ␈↓αn2␈↓↓ + ␈↓∧1␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βaa ␈↓αz␈↓↓ ␈↓βeq ␈↓∧3␈↓↓ ␈↓βthen␈↓↓ <␈↓αreverse␈↓↓ ␈↓αcompc␈↓↓[␈↓βda ␈↓αz␈↓↓, ␈↓αn2␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓], ␈↓αloadac␈↓↓[␈↓βd ␈↓αz␈↓↓, ␈↓αm2␈↓↓, ␈↓αn2␈↓↓ + ␈↓∧1␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓]>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βaa ␈↓αz␈↓↓ ␈↓βeq ␈↓∧5␈↓↓ ␈↓βthen␈↓↓ ␈↓αloadac␈↓↓[␈↓βd ␈↓αz␈↓↓, ␈↓∧1␈↓↓, ␈↓αn2␈↓↓ + ␈↓∧1␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse␈↓↓ <␈↓∧MOVE␈↓↓, ␈↓αn2␈↓↓, ␈↓αm2␈↓↓, ␈↓∧P␈↓↓> . ␈↓αloadac␈↓↓[␈↓βd ␈↓αz␈↓↓, ␈↓αm2␈↓↓ + ␈↓∧1␈↓↓, ␈↓αn2␈↓↓ + ␈↓∧1␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αcomplis␈↓↓[␈↓αz␈↓↓, ␈↓αm␈↓↓, ␈↓αk␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βn ␈↓αz␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL
␈↓ ↓H␈↓∧␈↓ α_␈↓βelse if␈↓↓ ␈↓βaa ␈↓αz␈↓↓ ␈↓βeq ␈↓∧4␈↓↓ ␈↓βthen␈↓↓ <␈↓αcompexp␈↓↓[␈↓βda ␈↓αz␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓], ((␈↓∧PUSH␈↓↓ ␈↓∧P␈↓↓ ␈↓∧1␈↓↓)), ␈↓αcomplis␈↓↓[␈↓βd ␈↓αz␈↓↓, ␈↓αm␈↓↓ - ␈↓∧1␈↓↓, ␈↓αk␈↓↓ + ␈↓∧1␈↓↓, ␈↓αvpr␈↓↓]>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βaa ␈↓αz␈↓↓ ␈↓βeq ␈↓∧5␈↓↓ ␈↓βthen␈↓↓ <␈↓αcompexp␈↓↓[␈↓βda ␈↓αz␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓], ␈↓βif␈↓↓ ␈↓αk␈↓↓ ␈↓βeq ␈↓∧1␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL␈↓↓ ␈↓βelse␈↓↓ <<␈↓∧MOVE␈↓↓, ␈↓αk␈↓↓, ␈↓∧1␈↓↓>>>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse␈↓↓ ␈↓αcomplis␈↓↓[␈↓βd ␈↓αz␈↓↓, ␈↓αm␈↓↓, ␈↓αk␈↓↓ + ␈↓∧1␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αclassify␈↓↓ ␈↓αu␈↓↓ ← ␈↓αclass2␈↓↓[␈↓αclass1␈↓↓[␈↓αu␈↓↓, ␈↓∧NIL␈↓↓], ␈↓∧NIL␈↓↓, ␈↓∧T␈↓↓]
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :50
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αclass1␈↓↓[␈↓αu␈↓↓, ␈↓αv␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βn ␈↓αu␈↓↓ ␈↓βthen␈↓↓ ␈↓αv
␈↓ ↓H␈↓α␈↓ α_␈↓βelse if␈↓↓ ␈↓βat a ␈↓αu␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8[␈↓βif␈↓↓ ␈↓αequal␈↓↓[␈↓βa ␈↓αu␈↓↓, ␈↓∧NIL␈↓↓] ∨ ␈↓αequal␈↓↓[␈↓βa ␈↓αu␈↓↓, ␈↓∧T␈↓↓] ∨ ␈↓αnumberp␈↓↓ ␈↓βa ␈↓αu␈↓↓ ␈↓βthen␈↓↓ ␈↓αclass1␈↓↓[␈↓βd ␈↓αu␈↓↓, [␈↓∧0␈↓↓ . ␈↓βa ␈↓αu␈↓↓] . ␈↓αv␈↓↓]
␈↓ ↓H␈↓↓␈↓ αA␈↓βelse␈↓↓ ␈↓αclass1␈↓↓[␈↓βd ␈↓αu␈↓↓, [␈↓∧1␈↓↓ . ␈↓βa ␈↓αu␈↓↓] . ␈↓αv␈↓↓]]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓αequal␈↓↓[␈↓βaa ␈↓αu␈↓↓, ␈↓∧QUOTE␈↓↓] ␈↓βthen␈↓↓ ␈↓αclass1␈↓↓[␈↓βd ␈↓αu␈↓↓, [␈↓∧2␈↓↓ . ␈↓βa ␈↓αu␈↓↓] . ␈↓αv␈↓↓]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓αccchain␈↓↓ ␈↓βa ␈↓αu␈↓↓ ␈↓βthen␈↓↓ ␈↓αclass1␈↓↓[␈↓βd ␈↓αu␈↓↓, [␈↓∧3␈↓↓ . ␈↓βa ␈↓αu␈↓↓] . ␈↓αv␈↓↓]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse␈↓↓ ␈↓αclass1␈↓↓[␈↓βd ␈↓αu␈↓↓, [␈↓∧4␈↓↓ . ␈↓βa ␈↓αu␈↓↓] . ␈↓αv␈↓↓]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αclass2␈↓↓[␈↓αu␈↓↓, ␈↓αv␈↓↓, ␈↓αflg␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βn ␈↓αu␈↓↓ ␈↓βthen␈↓↓ ␈↓αv
␈↓ ↓H␈↓α␈↓ α_␈↓βelse if␈↓↓ ␈↓αflg␈↓↓ ∧ ␈↓βaa ␈↓αu␈↓↓ ␈↓βeq ␈↓∧4␈↓↓ ␈↓βthen␈↓↓ ␈↓αclass2␈↓↓[␈↓βd ␈↓αu␈↓↓, [␈↓∧5␈↓↓ . ␈↓βda ␈↓αu␈↓↓] . ␈↓αv␈↓↓, ␈↓∧NIL␈↓↓]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse␈↓↓ ␈↓αclass2␈↓↓[␈↓βd ␈↓αu␈↓↓, ␈↓βa ␈↓αu␈↓↓ . ␈↓αv␈↓↓, ␈↓αflg␈↓↓]
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αmkjrst␈↓↓ ␈↓αl␈↓↓ ← <<␈↓∧JRST␈↓↓, ␈↓∧0␈↓↓, ␈↓αl␈↓↓>>
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αcombool␈↓↓[␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αflg␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓αp␈↓↓ ␈↓βeq ␈↓∧T␈↓↓ ␈↓βthen␈↓↓ [␈↓βif␈↓↓ ␈↓αflg␈↓↓ ␈↓βthen␈↓↓ ␈↓αmkjrst␈↓↓ ␈↓αl␈↓↓ ␈↓βelse␈↓↓ ␈↓∧NIL␈↓↓]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βat ␈↓αp␈↓↓ ␈↓βthen␈↓↓ <␈↓αcompexp␈↓↓[␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓], <<␈↓βif␈↓↓ ␈↓αflg␈↓↓ ␈↓βthen␈↓↓ ␈↓∧JUMPN␈↓↓ ␈↓βelse␈↓↓ ␈↓∧JUMPE␈↓↓, ␈↓∧1␈↓↓, ␈↓αl␈↓↓>>>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αp␈↓↓ ␈↓βeq ␈↓∧EQ␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8<␈↓αcomplisa␈↓↓[␈↓βd ␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓], ␈↓βif␈↓↓ ␈↓αflg␈↓↓ ␈↓βthen␈↓↓ ((␈↓∧CAMN␈↓↓ ␈↓∧1␈↓↓ ␈↓∧2␈↓↓)) ␈↓βelse␈↓↓ ((␈↓∧CAME␈↓↓ ␈↓∧1␈↓↓ ␈↓∧2␈↓↓)), ␈↓αmkjrst␈↓↓ ␈↓αl␈↓↓>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αp␈↓↓ ␈↓βeq ␈↓∧AND␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8[␈↓βif␈↓↓ ¬␈↓αflg␈↓↓ ␈↓βthen␈↓↓ ␈↓αcompandor␈↓↓[␈↓βd ␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓∧NIL␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ αA␈↓βelse␈↓↓ {␈↓αgensym␈↓↓[]}[λ␈↓αl1␈↓↓. <␈↓αcompandor1␈↓↓[␈↓βd ␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αl1␈↓↓, ␈↓αl␈↓↓, ␈↓∧NIL␈↓↓, ␈↓αvpr␈↓↓], <<␈↓∧LABEL␈↓↓, ␈↓αl1␈↓↓>>>]]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αp␈↓↓ ␈↓βeq ␈↓∧OR␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8[␈↓βif␈↓↓ ␈↓αflg␈↓↓ ␈↓βthen␈↓↓ ␈↓αcompandor␈↓↓[␈↓βd ␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓∧T␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ αA␈↓βelse␈↓↓ {␈↓αgensym␈↓↓[]}[λ␈↓αl1␈↓↓. <␈↓αcompandor1␈↓↓[␈↓βd ␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αl1␈↓↓, ␈↓αl␈↓↓, ␈↓∧T␈↓↓, ␈↓αvpr␈↓↓], <<␈↓∧LABEL␈↓↓, ␈↓αl1␈↓↓>>>]]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αp␈↓↓ ␈↓βeq ␈↓∧NOT␈↓↓ ␈↓βthen␈↓↓ ␈↓αcombool␈↓↓[␈↓βad ␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ¬␈↓αflg␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αp␈↓↓ ␈↓βeq ␈↓∧NULL␈↓↓ ␈↓βthen␈↓↓
␈↓ ↓H␈↓↓␈↓ α8<␈↓αcompexp␈↓↓[␈↓βad ␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓], <<␈↓βif␈↓↓ ␈↓αflg␈↓↓ ␈↓βthen␈↓↓ ␈↓∧JUMPE␈↓↓ ␈↓βelse␈↓↓ ␈↓∧JUMPN␈↓↓, ␈↓∧1␈↓↓, ␈↓αl␈↓↓>>>
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse␈↓↓ <␈↓αcompexp␈↓↓[␈↓αp␈↓↓, ␈↓αm␈↓↓, ␈↓αvpr␈↓↓], <<␈↓βif␈↓↓ ␈↓αflg␈↓↓ ␈↓βthen␈↓↓ ␈↓∧JUMPN␈↓↓ ␈↓βelse␈↓↓ ␈↓∧JUMPE␈↓↓, ␈↓∧1␈↓↓, ␈↓αl␈↓↓>>>
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αcompandor␈↓↓[␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αflg␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βn ␈↓αu␈↓↓ ␈↓βthen␈↓↓ ␈↓∧NIL␈↓↓ ␈↓βelse␈↓↓ <␈↓αcombool␈↓↓[␈↓βa ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αflg␈↓↓, ␈↓αvpr␈↓↓], ␈↓αcompandor␈↓↓[␈↓βd ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αflg␈↓↓, ␈↓αvpr␈↓↓]>
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αcompandor1␈↓↓[␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αl2␈↓↓, ␈↓αflg␈↓↓, ␈↓αvpr␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βn ␈↓αu␈↓↓ ␈↓βthen␈↓↓ ␈↓αmkjrst␈↓↓ ␈↓αl2
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :51
␈↓ ↓H␈↓α␈↓ α_␈↓βelse if␈↓↓ ␈↓βn d ␈↓αu␈↓↓ ␈↓βthen␈↓↓ ␈↓αcombool␈↓↓[␈↓βa ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl2␈↓↓, ¬␈↓αflg␈↓↓, ␈↓αvpr␈↓↓]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse␈↓↓ <␈↓αcombool␈↓↓[␈↓βa ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αflg␈↓↓, ␈↓αvpr␈↓↓], ␈↓αcompandor1␈↓↓[␈↓βd ␈↓αu␈↓↓, ␈↓αm␈↓↓, ␈↓αl␈↓↓, ␈↓αl2␈↓↓, ␈↓αflg␈↓↓, ␈↓αvpr␈↓↓]>
␈↓ ↓H␈↓↓␈↓ ↓x␈↓αflat␈↓↓[␈↓αu␈↓↓, ␈↓αs␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓βif␈↓↓ ␈↓βn ␈↓αu␈↓↓ ␈↓βthen␈↓↓ ␈↓αs
␈↓ ↓H␈↓α␈↓ α_␈↓βelse if␈↓↓ ␈↓βn a ␈↓αu␈↓↓ ␈↓βthen␈↓↓ ␈↓αflat␈↓↓[␈↓βd ␈↓αu␈↓↓, ␈↓αs␈↓↓]
␈↓ ↓H␈↓↓␈↓ α_␈↓βelse if␈↓↓ ␈↓βa ␈↓αu␈↓↓ ␈↓βeq ␈↓∧LABEL␈↓↓ ␈↓βthen␈↓↓ ␈↓βad ␈↓αu␈↓↓ . ␈↓αs
␈↓ ↓H␈↓α␈↓ α_␈↓βelse if␈↓↓ ␈↓βat a ␈↓αu␈↓↓ ␈↓βthen␈↓↓ ␈↓αu␈↓↓ . ␈↓αs
␈↓ ↓H␈↓α␈↓ α_␈↓βelse␈↓↓ ␈↓αflat␈↓↓[␈↓βa ␈↓αu␈↓↓, ␈↓αflat␈↓↓[␈↓βd ␈↓αu␈↓↓, ␈↓αs␈↓↓]]
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :52
␈↓ ↓H␈↓Annotated listing of LCOM0:
␈↓ ↓H␈↓(DEFPROP LC0FNS
␈↓ ↓H␈↓ (LC0FNS COMPL
␈↓ ↓H␈↓ COMP
␈↓ ↓H␈↓ PRUP
␈↓ ↓H␈↓ MKPUSH
␈↓ ↓H␈↓ COMPEXP
␈↓ ↓H␈↓ COMPLIS
␈↓ ↓H␈↓ LOADAC
␈↓ ↓H␈↓ COMCOND
␈↓ ↓H␈↓ COMBOOL
␈↓ ↓H␈↓ COMPANDOR)
␈↓ ↓H␈↓VALUE)
␈↓ ↓H␈↓~COMPL is the user-callable driver. It is a FEXPR. It takes as
␈↓ ↓H␈↓~ an argument a single file name, which must have no extension.
␈↓ ↓H␈↓~ EXPRs on a file called FILNAM will be compiled into LAP and
␈↓ ↓H␈↓~ written on the file FILNAM.LAP. Other types of function
␈↓ ↓H␈↓~ definitions and non-definitions are simply copied to output.
␈↓ ↓H␈↓(DEFPROP COMPL
␈↓ ↓H␈↓ (LAMBDA(FILE)
␈↓ ↓H␈↓ (PROG (Z)
␈↓ ↓H␈↓ (EVAL
␈↓ ↓H␈↓ (CONS (QUOTE OUTPUT)
␈↓ ↓H␈↓ (CONS (QUOTE DSK:)
␈↓ ↓H␈↓ (LIST (CONS (CAR FILE) (QUOTE LAP))))))
␈↓ ↓H␈↓ (EVAL (CONS (QUOTE INPUT) (CONS (QUOTE DSK:) FILE)))
␈↓ ↓H␈↓ (INC T NIL)
␈↓ ↓H␈↓ LOOP (SETQ Z (ERRSET (READ)))
␈↓ ↓H␈↓ (COND ((ATOM Z) (GO DONE)))
␈↓ ↓H␈↓ (SETQ Z (CAR Z))
␈↓ ↓H␈↓ (COND ((OR (EQ (CAR Z) (QUOTE DE))
␈↓ ↓H␈↓ (AND (EQ (CAR Z) (QUOTE DEFPROP))
␈↓ ↓H␈↓ (EQ (CADDDR Z) (QUOTE EXPR))))
␈↓ ↓H␈↓ (PROG (PROG)
␈↓ ↓H␈↓ (SETQ PROG
␈↓ ↓H␈↓ (COND ((EQ (CAR Z) (QUOTE DE))
␈↓ ↓H␈↓ (COMP (CADR Z)
␈↓ ↓H␈↓ (CADDR Z)
␈↓ ↓H␈↓ (CADDDR Z)))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (COMP (CADR Z)
␈↓ ↓H␈↓ (CADR (CADDR Z))
␈↓ ↓H␈↓ (CADDR (CADDR Z))))))
␈↓ ↓H␈↓ (OUTC T NIL)
␈↓ ↓H␈↓ (MAPC (FUNCTION PRINT) PROG)
␈↓ ↓H␈↓ (OUTC NIL NIL)
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :53
␈↓ ↓H␈↓ (PRINT (LIST (CADR Z) (LENGTH PROG)))))
␈↓ ↓H␈↓ (T (OUTC T NIL) (PRINT Z) (OUTC NIL NIL)))
␈↓ ↓H␈↓ (GO LOOP)
␈↓ ↓H␈↓ DONE (OUTC T NIL)
␈↓ ↓H␈↓ (OUTC NIL T)
␈↓ ↓H␈↓ (INC NIL T)
␈↓ ↓H␈↓ (RETURN (QUOTE ENDCOMP))))
␈↓ ↓H␈↓FEXPR)
␈↓ ↓H␈↓~COMP compiles a single function definition, returning a list of
␈↓ ↓H␈↓~ the LAP code corresponding to the definition.
␈↓ ↓H␈↓~ FN is the atomic name of the function being compiled.
␈↓ ↓H␈↓~ VARS is the formal parameter list for the function.
␈↓ ↓H␈↓~ EXP is the function body.
␈↓ ↓H␈↓(DEFPROP COMP
␈↓ ↓H␈↓ (LAMBDA(FN VARS EXP)
␈↓ ↓H␈↓ ((LAMBDA(N)
␈↓ ↓H␈↓ (APPEND (LIST (LIST (QUOTE LAP) FN (QUOTE SUBR)))
␈↓ ↓H␈↓ (MKPUSH N 1)
␈↓ ↓H␈↓ (COMPEXP EXP (MINUS N) (PRUP VARS 1))
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE C) N 0 N 0)))
␈↓ ↓H␈↓ (QUOTE ((POPJ P) NIL))))
␈↓ ↓H␈↓ (LENGTH VARS)))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~PRUP returns an A-LIST formed by pairing successive elements of
␈↓ ↓H␈↓~ VARS with consecutive integers beginning with N.
␈↓ ↓H␈↓(DEFPROP PRUP
␈↓ ↓H␈↓ (LAMBDA(VARS N)
␈↓ ↓H␈↓ (COND ((NULL VARS) NIL)
␈↓ ↓H␈↓ (T (CONS (CONS (CAR VARS) N) (PRUP (CDR VARS) (PLUS N 1))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~MKPUSH returns a list of N (PUSH P i) instructions, where i runs
␈↓ ↓H␈↓~ from M to M+N-1. Used to push arguments onto the stack.
␈↓ ↓H␈↓(DEFPROP MKPUSH
␈↓ ↓H␈↓ (LAMBDA(N M)
␈↓ ↓H␈↓ (COND ((LESSP N M) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (CONS (LIST (QUOTE PUSH) (QUOTE P) M)
␈↓ ↓H␈↓ (MKPUSH N (PLUS M 1))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~COMPEXP is the heart of LCOM0. It determines precisely
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :54
␈↓ ↓H␈↓~ what an expression is, and compiles appropriate code
␈↓ ↓H␈↓~ for it. It returns a list of that code.
␈↓ ↓H␈↓~ EXP is the expression to be compiled.
␈↓ ↓H␈↓~ M is minus the number of entries on the stack. When
␈↓ ↓H␈↓~ added to a value retrieved from the A-LIST VPR, it
␈↓ ↓H␈↓~ can be used to locate a variable on the stack.
␈↓ ↓H␈↓~ VPR is an A-LIST, associating variable names with
␈↓ ↓H␈↓~ numbers which, when added to M, give stack offsets.
␈↓ ↓H␈↓~ Both M and VPR maintain these definitions throughout.
␈↓ ↓H␈↓(DEFPROP COMPEXP
␈↓ ↓H␈↓ (LAMBDA(EXP M VPR)
␈↓ ↓H␈↓ (COND ((NULL EXP) (QUOTE ((MOVEI 1 0)))) ~NIL
␈↓ ↓H␈↓ ((EQ EXP T) (QUOTE ((MOVEI 1 (QUOTE T))))) ~T
␈↓ ↓H␈↓ ((ATOM EXP) ~variable
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE MOVE)
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ (PLUS M (CDR (ASSOC EXP VPR)))
␈↓ ↓H␈↓ (QUOTE P))))
␈↓ ↓H␈↓ ((OR (EQ (CAR EXP) (QUOTE AND)) ~boolean expression
␈↓ ↓H␈↓ (EQ (CAR EXP) (QUOTE OR))
␈↓ ↓H␈↓ (EQ (CAR EXP) (QUOTE NOT)))
␈↓ ↓H␈↓ ((LAMBDA(L1 L2)
␈↓ ↓H␈↓ (APPEND
␈↓ ↓H␈↓ (COMBOOL EXP M L1 NIL VPR)
␈↓ ↓H␈↓ (LIST (QUOTE (MOVEI 1 (QUOTE T)))
␈↓ ↓H␈↓ (LIST (QUOTE JRST) 0 L2)
␈↓ ↓H␈↓ L1
␈↓ ↓H␈↓ (QUOTE (MOVEI 1 0))
␈↓ ↓H␈↓ L2)))
␈↓ ↓H␈↓ (GENSYM)
␈↓ ↓H␈↓ (GENSYM)))
␈↓ ↓H␈↓ ((EQ (CAR EXP) (QUOTE COND)) ~COND
␈↓ ↓H␈↓ (COMCOND (CDR EXP) M (GENSYM) VPR))
␈↓ ↓H␈↓ ((EQ (CAR EXP) (QUOTE QUOTE)) ~QUOTE
␈↓ ↓H␈↓ (LIST (LIST (QUOTE MOVEI) 1 EXP)))
␈↓ ↓H␈↓ ((ATOM (CAR EXP)) ~function call
␈↓ ↓H␈↓ ((LAMBDA(N)
␈↓ ↓H␈↓ (APPEND
␈↓ ↓H␈↓ (COMPLIS (CDR EXP) M VPR)
␈↓ ↓H␈↓ (LOADAC (DIFFERENCE 1 N) 1)
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :55
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE C) N 0 N 0)))
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE CALL)
␈↓ ↓H␈↓ N
␈↓ ↓H␈↓ (LIST (QUOTE E) (CAR EXP))
␈↓ ↓H␈↓ (QUOTE S)))))
␈↓ ↓H␈↓ (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ ((EQ (CAAR EXP) (QUOTE LAMBDA)) ~LAMBDA expression
␈↓ ↓H␈↓ ((LAMBDA(N)
␈↓ ↓H␈↓ (APPEND
␈↓ ↓H␈↓ (COMPLIS (CDR EXP) M VPR)
␈↓ ↓H␈↓ (COMPEXP
␈↓ ↓H␈↓ (CADDAR EXP)
␈↓ ↓H␈↓ (DIFFERENCE M N)
␈↓ ↓H␈↓ (APPEND (PRUP (CADAR EXP) (DIFFERENCE 1 M)) VPR))
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE C) N 0 N 0)))))
␈↓ ↓H␈↓ (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ (T NIL))) ~oops
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~COMPLIS compiles code to evaluate each expression in a list of
␈↓ ↓H␈↓~ expressions and to push those values onto the stack. It
␈↓ ↓H␈↓~ returns a list of that code. It is used to compile code
␈↓ ↓H␈↓~ to evaluate arguments to called functions or LAMBDA expressions.
␈↓ ↓H␈↓~ U is a list of expressions.
␈↓ ↓H␈↓(DEFPROP COMPLIS
␈↓ ↓H␈↓ (LAMBDA(U M VPR)
␈↓ ↓H␈↓ (COND ((NULL U) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (APPEND (COMPEXP (CAR U) M VPR)
␈↓ ↓H␈↓ (QUOTE ((PUSH P 1)))
␈↓ ↓H␈↓ (COMPLIS (CDR U) (DIFFERENCE M 1) VPR)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~LOADAC returns a list of (MOVE i j P) instructions, loading
␈↓ ↓H␈↓~ consecutive accumulators from the top of the stack.
␈↓ ↓H␈↓~ K indexes the accumulator loaded.
␈↓ ↓H␈↓~ N indexes the stack offset.
␈↓ ↓H␈↓(DEFPROP LOADAC
␈↓ ↓H␈↓ (LAMBDA(N K)
␈↓ ↓H␈↓ (COND ((GREATERP N 0) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :56
␈↓ ↓H␈↓ (CONS (LIST (QUOTE MOVE) K N (QUOTE P))
␈↓ ↓H␈↓ (LOADAC (PLUS N 1) (PLUS K 1))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~COMCOND compiles a COND.
␈↓ ↓H␈↓~ U is a list of clauses in the COND.
␈↓ ↓H␈↓~ L is a label to be emitted at the end of all code for
␈↓ ↓H␈↓~ the COND.
␈↓ ↓H␈↓(DEFPROP COMCOND
␈↓ ↓H␈↓ (LAMBDA(U M L VPR)
␈↓ ↓H␈↓ (COND ((NULL U) (LIST L))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ ((LAMBDA(L1)
␈↓ ↓H␈↓ (APPEND (COMBOOL (CAAR U) M L1 NIL VPR)
␈↓ ↓H␈↓ (COMPEXP (CADAR U) M VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE JRST) L) L1)
␈↓ ↓H␈↓ (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ (GENSYM)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~COMBOOL compiles code for a single predicate. That is, the
␈↓ ↓H␈↓~ code generated evaluates the predicate and branches somewhere,
␈↓ ↓H␈↓~ depending on the value.
␈↓ ↓H␈↓~ P is the predicate.
␈↓ ↓H␈↓~ L is a label which represents the branch point.
␈↓ ↓H␈↓~ FLG is a flag. If FLG is NIL, code is to fall thru on non-NIL
␈↓ ↓H␈↓~ result and branch to L on NIL result. If FLG is non-NIL,
␈↓ ↓H␈↓~ code is to fall thru on NIL result and branch to L on
␈↓ ↓H␈↓~ non-NIL result.
␈↓ ↓H␈↓(DEFPROP COMBOOL
␈↓ ↓H␈↓ (LAMBDA(P M L FLG VPR)
␈↓ ↓H␈↓ (COND ((ATOM P) ~simple variable
␈↓ ↓H␈↓ (APPEND
␈↓ ↓H␈↓ (COMPEXP P M VPR)
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE))) 1 L))))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE AND)) ~conjunction
␈↓ ↓H␈↓ (COND ((NOT FLG) (COMPANDOR (CDR P) M L NIL VPR))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ ((LAMBDA(L1)
␈↓ ↓H␈↓ (APPEND (COMPANDOR (CDR P) M L1 NIL VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE JRST) 0 L))
␈↓ ↓H␈↓ (LIST L1)))
␈↓ ↓H␈↓ (GENSYM)))))
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :57
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE OR)) ~disjunction
␈↓ ↓H␈↓ (COND (FLG (COMPANDOR (CDR P) M L T VPR))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ ((LAMBDA(L1)
␈↓ ↓H␈↓ (APPEND (COMPANDOR (CDR P) M L1 T VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE JRST) 0 L))
␈↓ ↓H␈↓ (LIST L1)))
␈↓ ↓H␈↓ (GENSYM)))))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE NOT)) ~negation
␈↓ ↓H␈↓ (COMBOOL (CADR P) M L (NOT FLG) VPR))
␈↓ ↓H␈↓ (T ~other expression
␈↓ ↓H␈↓ (APPEND
␈↓ ↓H␈↓ (COMPEXP P M VPR)
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE)))
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ L))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~COMPANDOR compiles code for lists of predicates connected
␈↓ ↓H␈↓~ conjunctively or disjunctively.
␈↓ ↓H␈↓~ U is a list of predicates.
␈↓ ↓H␈↓~ L is a label.
␈↓ ↓H␈↓~ FLG is a flag. If FLG is NIL, we are to fall thru on non-NIL
␈↓ ↓H␈↓~ results and branch to L on NIL results (AND case). If FLG
␈↓ ↓H␈↓~ is non-NIL, we are to fall thru on NIL results and branch
␈↓ ↓H␈↓~ to L on non-NIL results (OR case).
␈↓ ↓H␈↓(DEFPROP COMPANDOR
␈↓ ↓H␈↓ (LAMBDA(U M L FLG VPR)
␈↓ ↓H␈↓ (COND ((NULL U) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (APPEND (COMBOOL (CAR U) M L FLG VPR)
␈↓ ↓H␈↓ (COMPANDOR (CDR U) M L FLG VPR)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :58
␈↓ ↓H␈↓LCOM4:
␈↓ ↓H␈↓(DEFPROP COMPFCNS
␈↓ ↓H␈↓ (COMPFCNS COMPL
␈↓ ↓H␈↓ COMP
␈↓ ↓H␈↓ SUBSTACK
␈↓ ↓H␈↓ PRUP
␈↓ ↓H␈↓ MKPUSH
␈↓ ↓H␈↓ COMPEXP
␈↓ ↓H␈↓ STACKUP
␈↓ ↓H␈↓ CCCHAIN
␈↓ ↓H␈↓ COMPC
␈↓ ↓H␈↓ COMCOND
␈↓ ↓H␈↓ COMPLISA
␈↓ ↓H␈↓ CCOUNT
␈↓ ↓H␈↓ LOADAC
␈↓ ↓H␈↓ COMPLIS
␈↓ ↓H␈↓ CLASSIFY
␈↓ ↓H␈↓ CLASS1
␈↓ ↓H␈↓ CLASS2
␈↓ ↓H␈↓ MKJRST
␈↓ ↓H␈↓ COMBOOL
␈↓ ↓H␈↓ COMPANDOR
␈↓ ↓H␈↓ COMPANDOR1
␈↓ ↓H␈↓ FLAT)
␈↓ ↓H␈↓VALUE)
␈↓ ↓H␈↓(DEFPROP COMPL
␈↓ ↓H␈↓ (LAMBDA(FILE)
␈↓ ↓H␈↓ (PROG (Z)
␈↓ ↓H␈↓ (EVAL
␈↓ ↓H␈↓ (CONS (QUOTE OUTPUT)
␈↓ ↓H␈↓ (CONS (QUOTE DSK:)
␈↓ ↓H␈↓ (LIST (CONS (CAR FILE) (QUOTE LAP))))))
␈↓ ↓H␈↓ (EVAL (CONS (QUOTE INPUT) (CONS (QUOTE DSK:) FILE)))
␈↓ ↓H␈↓ (INC (QUOTE T) NIL)
␈↓ ↓H␈↓ (OUTC T NIL)
␈↓ ↓H␈↓ LOOP (SETQ Z (ERRSET (READ)))
␈↓ ↓H␈↓ (COND ((ATOM Z) (GO DONE)) ((QUOTE T) (QUOTE NIL)))
␈↓ ↓H␈↓ (SETQ Z (CAR Z))
␈↓ ↓H␈↓ (COND ((OR (EQ (CAR Z) (QUOTE DE))
␈↓ ↓H␈↓ (AND (EQ (CAR Z) (QUOTE DEFPROP))
␈↓ ↓H␈↓ (EQ (CADDDR Z) (QUOTE EXPR))))
␈↓ ↓H␈↓ (PROG (PROG)
␈↓ ↓H␈↓ (SETQ PROG
␈↓ ↓H␈↓ (COND ((EQ (CAR Z) (QUOTE DE))
␈↓ ↓H␈↓ (COMP (CADR Z)
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :59
␈↓ ↓H␈↓ (CADDR Z)
␈↓ ↓H␈↓ (CADDDR Z)))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (COMP (CADR Z)
␈↓ ↓H␈↓ (CADR (CADDR Z))
␈↓ ↓H␈↓ (CADDR (CADDR Z))))))
␈↓ ↓H␈↓ (MAPC (FUNCTION PRINT) PROG)
␈↓ ↓H␈↓ (OUTC NIL NIL)
␈↓ ↓H␈↓ (PRINT (LIST (CADR Z) (LENGTH PROG)))
␈↓ ↓H␈↓ (OUTC T NIL)))
␈↓ ↓H␈↓ (T (PRINT Z)))
␈↓ ↓H␈↓ (GO LOOP)
␈↓ ↓H␈↓ DONE (OUTC NIL T)
␈↓ ↓H␈↓ (INC NIL T)
␈↓ ↓H␈↓ (RETURN (QUOTE ENDCOMP))))
␈↓ ↓H␈↓FEXPR)
␈↓ ↓H␈↓(DEFPROP COMP
␈↓ ↓H␈↓ (LAMBDA(FN VARS EXP)
␈↓ ↓H␈↓ ((LAMBDA(VPR N)
␈↓ ↓H␈↓ (FLAT (LIST (LIST (LIST (QUOTE LAP) FN (QUOTE SUBR)))
␈↓ ↓H␈↓ (MKPUSH N 1)
␈↓ ↓H␈↓ (COMPEXP EXP (MINUS N) VPR)
␈↓ ↓H␈↓ (SUBSTACK N)
␈↓ ↓H␈↓ (QUOTE ((POPJ P) (LABEL NIL))))
␈↓ ↓H␈↓ NIL))
␈↓ ↓H␈↓ (PRUP VARS 1)
␈↓ ↓H␈↓ (LENGTH VARS)))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP SUBSTACK
␈↓ ↓H␈↓ (LAMBDA(N)
␈↓ ↓H␈↓ (COND ((EQ N 0) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE C) N 0 N 0))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP PRUP
␈↓ ↓H␈↓ (LAMBDA(VARS N)
␈↓ ↓H␈↓ (COND ((NULL VARS) NIL)
␈↓ ↓H␈↓ (T (CONS (CONS (CAR VARS) N) (PRUP (CDR VARS) (PLUS N 1))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP MKPUSH
␈↓ ↓H␈↓ (LAMBDA(N M)
␈↓ ↓H␈↓ (COND ((LESSP N M) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :60
␈↓ ↓H␈↓ (CONS (LIST (QUOTE PUSH) (QUOTE P) M)
␈↓ ↓H␈↓ (MKPUSH N (PLUS M 1))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMPEXP
␈↓ ↓H␈↓ (LAMBDA(EXP M VPR)
␈↓ ↓H␈↓ (COND ((NULL EXP) (QUOTE ((MOVEI 1 0))))
␈↓ ↓H␈↓ ((OR (EQ EXP (QUOTE T)) (NUMBERP EXP))
␈↓ ↓H␈↓ (LIST (LIST (QUOTE MOVEI) 1 (LIST (QUOTE QUOTE) EXP))))
␈↓ ↓H␈↓ ((ATOM EXP)
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE MOVE)
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ (PLUS M (CDR (ASSOC EXP VPR)))
␈↓ ↓H␈↓ (QUOTE P))))
␈↓ ↓H␈↓ ((EQ (CAR EXP) (QUOTE CAR))
␈↓ ↓H␈↓ (COND ((ATOM (CADR EXP))
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE HLRZ@)
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ (PLUS M (CDR (ASSOC (CADR EXP) VPR)))
␈↓ ↓H␈↓ (QUOTE P))))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (LIST (COMPEXP (CADR EXP) M VPR)
␈↓ ↓H␈↓ (QUOTE ((HLRZ@ 1 1)))))))
␈↓ ↓H␈↓ ((EQ (CAR EXP) (QUOTE CDR))
␈↓ ↓H␈↓ (COND ((ATOM (CADR EXP))
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE HRRZ@)
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ (PLUS M (CDR (ASSOC (CADR EXP) VPR)))
␈↓ ↓H␈↓ (QUOTE P))))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (LIST (COMPEXP (CADR EXP) M VPR)
␈↓ ↓H␈↓ (QUOTE ((HRRZ@ 1 1)))))))
␈↓ ↓H␈↓ ((OR (EQ (CAR EXP) (QUOTE AND))
␈↓ ↓H␈↓ (EQ (CAR EXP) (QUOTE OR))
␈↓ ↓H␈↓ (EQ (CAR EXP) (QUOTE NOT))
␈↓ ↓H␈↓ (EQ (CAR EXP) (QUOTE EQ)))
␈↓ ↓H␈↓ ((LAMBDA(L1 L2)
␈↓ ↓H␈↓ (LIST (COMBOOL EXP M L1 NIL VPR)
␈↓ ↓H␈↓ (LIST (QUOTE (MOVEI 1 (QUOTE T)))
␈↓ ↓H␈↓ (LIST (QUOTE JRST) 0 L2)
␈↓ ↓H␈↓ (LIST (QUOTE LABEL) L1)
␈↓ ↓H␈↓ (QUOTE (MOVEI 1 0))
␈↓ ↓H␈↓ (LIST (QUOTE LABEL) L2))))
␈↓ ↓H␈↓ (GENSYM)
␈↓ ↓H␈↓ (GENSYM)))
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :61
␈↓ ↓H␈↓ ((EQ (CAR EXP) (QUOTE COND))
␈↓ ↓H␈↓ (COMCOND (CDR EXP) M (GENSYM) VPR))
␈↓ ↓H␈↓ ((EQ (CAR EXP) (QUOTE QUOTE))
␈↓ ↓H␈↓ (LIST (LIST (QUOTE MOVEI) 1 EXP)))
␈↓ ↓H␈↓ ((ATOM (CAR EXP))
␈↓ ↓H␈↓ (LIST (COMPLISA (CDR EXP) M VPR)
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE CALL)
␈↓ ↓H␈↓ (LENGTH (CDR EXP))
␈↓ ↓H␈↓ (LIST (QUOTE E) (CAR EXP))
␈↓ ↓H␈↓ (QUOTE S)))))
␈↓ ↓H␈↓ ((EQ (CAAR EXP) (QUOTE LAMBDA))
␈↓ ↓H␈↓ ((LAMBDA(N)
␈↓ ↓H␈↓ (LIST (STACKUP (CDR EXP) M VPR)
␈↓ ↓H␈↓ (COMPEXP
␈↓ ↓H␈↓ (CADDAR EXP)
␈↓ ↓H␈↓ (DIFFERENCE M N)
␈↓ ↓H␈↓ (APEND (PRUP (CADAR EXP) (DIFFERENCE 1 M)) VPR))
␈↓ ↓H␈↓ (SUBSTACK N)))
␈↓ ↓H␈↓ (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ ((QUOTE T) (QUOTE NIL))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP STACKUP
␈↓ ↓H␈↓ (LAMBDA(U M VPR)
␈↓ ↓H␈↓ (COND ((NULL U) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (LIST (COMPEXP (CAR U) M VPR)
␈↓ ↓H␈↓ (QUOTE ((PUSH P 1)))
␈↓ ↓H␈↓ (STACKUP (CDR U) (DIFFERENCE M 1) VPR)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP CCCHAIN
␈↓ ↓H␈↓ (LAMBDA(EXP)
␈↓ ↓H␈↓ (AND (OR (EQ (CAR EXP) (QUOTE CAR)) (EQ (CAR EXP) (QUOTE CDR)))
␈↓ ↓H␈↓ (OR (ATOM (CADR EXP)) (CCCHAIN (CADR EXP)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMPC
␈↓ ↓H␈↓ (LAMBDA(EXP N2 M VPR)
␈↓ ↓H␈↓ (COND ((ATOM EXP) (ERR (QUOTE COMPC)))
␈↓ ↓H␈↓ ((EQ (CAR EXP) (QUOTE CAR))
␈↓ ↓H␈↓ (COND ((ATOM (CADR EXP))
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE HLRZ@)
␈↓ ↓H␈↓ N2
␈↓ ↓H␈↓ (PLUS M (CDR (ASSOC (CADR EXP) VPR)))
␈↓ ↓H␈↓ (QUOTE P))))
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :62
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (CONS (LIST (QUOTE HLRZ@) N2 N2)
␈↓ ↓H␈↓ (COMPC (CADR EXP) N2 M VPR)))))
␈↓ ↓H␈↓ ((ATOM (CADR EXP))
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE HRRZ@)
␈↓ ↓H␈↓ N2
␈↓ ↓H␈↓ (PLUS M (CDR (ASSOC (CADR EXP) VPR)))
␈↓ ↓H␈↓ (QUOTE P))))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (CONS (LIST (QUOTE HRRZ@) N2 N2)
␈↓ ↓H␈↓ (COMPC (CADR EXP) N2 M VPR)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMCOND
␈↓ ↓H␈↓ (LAMBDA(U M L VPR)
␈↓ ↓H␈↓ (COND ((NULL U) (LIST (LIST (QUOTE LABEL) L)))
␈↓ ↓H␈↓ ((AND (NOT (ATOM (CAAR U)))
␈↓ ↓H␈↓ (EQ (CAAAR U) (QUOTE NULL))
␈↓ ↓H␈↓ (NULL (CADAR U)))
␈↓ ↓H␈↓ (LIST (COMPEXP (CADAAR U) M VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE JUMPE) 1 L))
␈↓ ↓H␈↓ (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ ((EQ (CAAR U) (QUOTE T))
␈↓ ↓H␈↓ (LIST (COMPEXP (CADAR U) M VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE LABEL) L))))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ ((LAMBDA(L1)
␈↓ ↓H␈↓ (LIST (COMBOOL (CAAR U) M L1 NIL VPR)
␈↓ ↓H␈↓ (COMPEXP (CADAR U) M VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE JRST) 0 L)
␈↓ ↓H␈↓ (LIST (QUOTE LABEL) L1))
␈↓ ↓H␈↓ (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ (GENSYM)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMPLISA
␈↓ ↓H␈↓ (LAMBDA(U M VPR)
␈↓ ↓H␈↓ ((LAMBDA(Z)
␈↓ ↓H␈↓ (LIST (COMPLIS Z M 1 VPR)
␈↓ ↓H␈↓ (LOADAC Z
␈↓ ↓H␈↓ (DIFFERENCE 1 (CCOUNT Z))
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ (DIFFERENCE M (CCOUNT Z))
␈↓ ↓H␈↓ VPR)
␈↓ ↓H␈↓ (SUBSTACK (CCOUNT Z))))
␈↓ ↓H␈↓ (CLASSIFY U)))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :63
␈↓ ↓H␈↓(DEFPROP CCOUNT
␈↓ ↓H␈↓ (LAMBDA(Z)
␈↓ ↓H␈↓ (COND ((NULL Z) 0)
␈↓ ↓H␈↓ ((EQ (CAAR Z) 4) (PLUS 1 (CCOUNT (CDR Z))))
␈↓ ↓H␈↓ (T (CCOUNT (CDR Z)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP LOADAC
␈↓ ↓H␈↓ (LAMBDA(Z M2 N2 M VPR)
␈↓ ↓H␈↓ (COND ((NULL Z) NIL)
␈↓ ↓H␈↓ ((EQ (CAAR Z) 1)
␈↓ ↓H␈↓ (CONS (LIST (QUOTE MOVE)
␈↓ ↓H␈↓ N2
␈↓ ↓H␈↓ (PLUS M (CDR (ASSOC (CDAR Z) VPR)))
␈↓ ↓H␈↓ (QUOTE P))
␈↓ ↓H␈↓ (LOADAC (CDR Z) M2 (PLUS N2 1) M VPR)))
␈↓ ↓H␈↓ ((EQ (CAAR Z) 0)
␈↓ ↓H␈↓ (CONS (LIST (QUOTE MOVEI) N2 (LIST (QUOTE QUOTE) (CDAR Z)))
␈↓ ↓H␈↓ (LOADAC (CDR Z) M2 (PLUS N2 1) M VPR)))
␈↓ ↓H␈↓ ((EQ (CAAR Z) 2)
␈↓ ↓H␈↓ (CONS (LIST (QUOTE MOVEI) N2 (CDAR Z))
␈↓ ↓H␈↓ (LOADAC (CDR Z) M2 (PLUS N2 1) M VPR)))
␈↓ ↓H␈↓ ((EQ (CAAR Z) 3)
␈↓ ↓H␈↓ (LIST (REVERSE (COMPC (CDAR Z) N2 M VPR))
␈↓ ↓H␈↓ (LOADAC (CDR Z) M2 (PLUS N2 1) M VPR)))
␈↓ ↓H␈↓ ((EQ (CAAR Z) 5) (LOADAC (CDR Z) 1 (PLUS N2 1) M VPR))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (CONS (LIST (QUOTE MOVE) N2 M2 (QUOTE P))
␈↓ ↓H␈↓ (LOADAC (CDR Z) (PLUS M2 1) (PLUS N2 1) M VPR)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMPLIS
␈↓ ↓H␈↓ (LAMBDA(Z M K VPR)
␈↓ ↓H␈↓ (COND ((NULL Z) NIL)
␈↓ ↓H␈↓ ((EQ (CAAR Z) 4)
␈↓ ↓H␈↓ (LIST (COMPEXP (CDAR Z) M VPR)
␈↓ ↓H␈↓ (QUOTE ((PUSH P 1)))
␈↓ ↓H␈↓ (COMPLIS (CDR Z) (DIFFERENCE M 1) (PLUS K 1) VPR)))
␈↓ ↓H␈↓ ((EQ (CAAR Z) 5)
␈↓ ↓H␈↓ (LIST (COMPEXP (CDAR Z) M VPR)
␈↓ ↓H␈↓ (COND ((EQ K 1) NIL)
␈↓ ↓H␈↓ (T (LIST (LIST (QUOTE MOVE) K 1))))))
␈↓ ↓H␈↓ (T (COMPLIS (CDR Z) M (PLUS K 1) VPR))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP CLASSIFY
␈↓ ↓H␈↓ (LAMBDA (U) (CLASS2 (CLASS1 U NIL) NIL T))
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :64
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP CLASS1
␈↓ ↓H␈↓ (LAMBDA(U V)
␈↓ ↓H␈↓ (COND ((NULL U) V)
␈↓ ↓H␈↓ ((ATOM (CAR U))
␈↓ ↓H␈↓ (COND ((OR (EQUAL (CAR U) (QUOTE NIL))
␈↓ ↓H␈↓ (EQUAL (CAR U) (QUOTE T))
␈↓ ↓H␈↓ (NUMBERP (CAR U)))
␈↓ ↓H␈↓ (CLASS1 (CDR U) (CONS (CONS 0 (CAR U)) V)))
␈↓ ↓H␈↓ (T (CLASS1 (CDR U) (CONS (CONS 1 (CAR U)) V)))))
␈↓ ↓H␈↓ ((EQUAL (CAAR U) (QUOTE QUOTE))
␈↓ ↓H␈↓ (CLASS1 (CDR U) (CONS (CONS 2 (CAR U)) V)))
␈↓ ↓H␈↓ ((CCCHAIN (CAR U))
␈↓ ↓H␈↓ (CLASS1 (CDR U) (CONS (CONS 3 (CAR U)) V)))
␈↓ ↓H␈↓ (T (CLASS1 (CDR U) (CONS (CONS 4 (CAR U)) V)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP CLASS2
␈↓ ↓H␈↓ (LAMBDA(U V FLG)
␈↓ ↓H␈↓ (COND ((NULL U) V)
␈↓ ↓H␈↓ ((AND FLG (EQ (CAAR U) 4))
␈↓ ↓H␈↓ (CLASS2 (CDR U) (CONS (CONS 5 (CDAR U)) V) NIL))
␈↓ ↓H␈↓ (T (CLASS2 (CDR U) (CONS (CAR U) V) FLG))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP MKJRST
␈↓ ↓H␈↓ (LAMBDA (L) (LIST (LIST (QUOTE JRST) 0 L)))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMBOOL
␈↓ ↓H␈↓ (LAMBDA(P M L FLG VPR)
␈↓ ↓H␈↓ (COND ((EQ P (QUOTE T)) (COND (FLG (MKJRST L)) (T NIL)))
␈↓ ↓H␈↓ ((ATOM P)
␈↓ ↓H␈↓ (LIST (COMPEXP P M VPR)
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE)))
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ L))))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE EQ))
␈↓ ↓H␈↓ (LIST (COMPLISA (CDR P) M VPR)
␈↓ ↓H␈↓ (COND (FLG (QUOTE ((CAMN 1 2))))
␈↓ ↓H␈↓ (T (QUOTE ((CAME 1 2)))))
␈↓ ↓H␈↓ (MKJRST L)))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE AND))
␈↓ ↓H␈↓ (COND ((NOT FLG) (COMPANDOR (CDR P) M L NIL VPR))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ ((LAMBDA(L1)
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :65
␈↓ ↓H␈↓ (LIST (COMPANDOR1 (CDR P) M L1 L NIL VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE LABEL) L1))))
␈↓ ↓H␈↓ (GENSYM)))))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE OR))
␈↓ ↓H␈↓ (COND (FLG (COMPANDOR (CDR P) M L T VPR))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ ((LAMBDA(L1)
␈↓ ↓H␈↓ (LIST (COMPANDOR1 (CDR P) M L1 L T VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE LABEL) L1))))
␈↓ ↓H␈↓ (GENSYM)))))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE NOT))
␈↓ ↓H␈↓ (COMBOOL (CADR P) M L (NOT FLG) VPR))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE NULL))
␈↓ ↓H␈↓ (LIST (COMPEXP (CADR P) M VPR)
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (COND (FLG (QUOTE JUMPE)) (T (QUOTE JUMPN)))
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ L))))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (LIST (COMPEXP P M VPR)
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE)))
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ L))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMPANDOR
␈↓ ↓H␈↓ (LAMBDA(U M L FLG VPR)
␈↓ ↓H␈↓ (COND ((NULL U) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (LIST (COMBOOL (CAR U) M L FLG VPR)
␈↓ ↓H␈↓ (COMPANDOR (CDR U) M L FLG VPR)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMPANDOR1
␈↓ ↓H␈↓ (LAMBDA(U M L L2 FLG VPR)
␈↓ ↓H␈↓ (COND ((NULL U) (MKJRST L2))
␈↓ ↓H␈↓ ((NULL (CDR U)) (COMBOOL (CAR U) M L2 (NOT FLG) VPR))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (LIST (COMBOOL (CAR U) M L FLG VPR)
␈↓ ↓H␈↓ (COMPANDOR1 (CDR U) M L L2 FLG VPR)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP FLAT
␈↓ ↓H␈↓ (LAMBDA(U S)
␈↓ ↓H␈↓ (COND ((NULL U) S)
␈↓ ↓H␈↓ ((NULL (CAR U)) (FLAT (CDR U) S))
␈↓ ↓H␈↓ ((EQ (CAR U) (QUOTE LABEL)) (CONS (CADR U) S))
␈↓ ↓H␈↓␈↓ ¬lCHAPTER III␈↓ :66
␈↓ ↓H␈↓ ((ATOM (CAR U)) (CONS U S))
␈↓ ↓H␈↓ (T (FLAT (CAR U) (FLAT (CDR U) S)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓␈↓ εP␈↓ :67
␈↓ ↓H␈↓β␈↓ ¬mCHAPTER IV
␈↓ ↓H␈↓β␈↓ ¬JCOMPUTABILITY
␈↓ ↓H␈↓1. ␈↓βThe function ␈↓αeval␈↓.
␈↓ ↓H␈↓ Except␈αfor␈α
speed␈αand␈αmemory␈α
size␈αall␈α
present␈αday␈αstored␈α
program␈αcomputers␈α
are␈αequivalent
␈↓ ↓H␈↓in␈αwhat␈αcomputations␈αthey␈αcan␈α
do.␈α A␈αprogram␈αwritten␈αfor␈α
one␈αcomputer␈αcan␈αbe␈αtranslated␈α
to␈αrun
␈↓ ↓H␈↓on␈αanother.␈α
Indeed,␈αone␈αcan␈α
write␈αa␈α
simulator␈αfor␈αone␈α
computer␈αto␈α
run␈αon␈αanother.␈α
To␈αput␈α
it␈αin
␈↓ ↓H␈↓commercial␈α∞terms,␈α∞no␈α∂computer␈α∞manufacturer␈α∞can␈α∞advertise␈α∂that␈α∞his␈α∞machine␈α∞can␈α∂do␈α∞calculations
␈↓ ↓H␈↓impossible on the machine made by his competitors.
␈↓ ↓H␈↓ This␈α
is␈αwell␈α
known␈αintuitively,␈α
and␈α
the␈αfirst␈α
mathematical␈αtheorem␈α
of␈αthis␈α
kind␈α
was␈αproved
␈↓ ↓H␈↓by␈αA.M.␈α
Turing␈α(1936),␈α
who␈αdefined␈αa␈α
primitive␈αkind␈α
of␈αcomputer␈αnow␈α
called␈αa␈α
Turing␈αmachine,
␈↓ ↓H␈↓and␈αshowed␈αhow␈αto␈αmake␈αa␈αuniversal␈αmachine␈αthat␈αcould␈αdo␈αany␈αcomputation␈αdone␈αby␈αany␈α
Turing
␈↓ ↓H␈↓machine␈α⊂when␈α⊂given␈α⊂a␈α⊂description␈α⊂of␈α⊂the␈α⊂machine␈α⊂to␈α⊂be␈α⊂simulated␈α⊂and␈α⊂the␈α⊂initial␈α⊂tape␈α⊂of␈α⊂the
␈↓ ↓H␈↓computation to be imitated.
␈↓ ↓H␈↓ In␈α∞LISP␈α∞the␈α∞function␈α∞␈↓αeval␈↓␈α
is␈α∞a␈α∞universal␈α∞LISP␈α∞function␈α
in␈α∞the␈α∞sense␈α∞that␈α∞any␈α
computation
␈↓ ↓H␈↓done by any LISP function can be done by ␈↓αeval␈↓ when ␈↓αeval␈↓ is given suitable arguments.
␈↓ ↓H␈↓ ␈↓αeval␈↓␈αhas␈αtwo␈αarguments␈αthe␈αfirst␈αof␈αwhich␈αis␈αa␈αLISP␈αexpression␈αin␈αthe␈αnotation␈αgiven␈αin␈αthe
␈↓ ↓H␈↓previous␈αsection,␈α
while␈αthe␈αsecond␈α
is␈αa␈αlist␈α
of␈αpairs␈αthat␈α
give␈αthe␈αvalues␈α
of␈αany␈αfree␈α
variables␈αthat
␈↓ ↓H␈↓may␈αoccur␈αin␈αthe␈αexpression.␈α Since␈αany␈αcomputation␈αcan␈αbe␈αdescribed␈αas␈αevaluating␈αan␈αexpression
␈↓ ↓H␈↓without␈αfree␈αvariables,␈αthe␈αsecond␈αargument␈αplays␈αa␈αrole␈αmainly␈αin␈αthe␈αrecursive␈αdefinition␈αof␈α␈↓αeval␈↓,
␈↓ ↓H␈↓and we can start our computations with the second argument NIL.
␈↓ ↓H␈↓ To␈αillustrate␈αthis,␈αsuppose␈αwe␈αwant␈αto␈αapply␈αthe␈αfunction␈α␈↓αalt␈↓␈αto␈αthe␈αlist␈α(A␈αB␈αC␈αD␈αE),␈αi.e.␈αwe
␈↓ ↓H␈↓wish to evaluate ␈↓αalt␈↓↓[␈↓∧(A B C D E)␈↓↓]␈↓. This can be obtained by computing
␈↓ ↓H␈↓ ␈↓αeval␈↓↓[␈↓∧((LABEL ALT (LAMBDA (X) (COND ((OR (NULL X) (NULL (CDR X))) X)
␈↓ ↓H␈↓∧(T (CONS (CAR X) (ALT (CDDR X))))))) (QUOTE (A B C D E)), NIL␈↓↓]␈↓,
␈↓ ↓H␈↓and␈αgives␈α
the␈αexpected␈α
result␈α(A␈α
C␈αE).␈α
The␈αsecond␈αargument␈α
of␈α␈↓αeval␈↓,␈α
taken␈αas␈α
NIL␈αin␈α
the␈αabove
␈↓ ↓H␈↓example␈α∞is␈α∞a␈α∞list␈α∂of␈α∞dotted␈α∞pairs␈α∞where␈α∂the␈α∞first␈α∞element␈α∞of␈α∞each␈α∂pair␈α∞is␈α∞an␈α∞atom␈α∂representing␈α∞a
␈↓ ↓H␈↓variable␈αand␈α
the␈αsecond␈α
element␈αis␈α
the␈αvalue␈αassigned␈α
to␈αthat␈α
variable.␈α A␈α
variable␈αmay␈αoccur␈α
more
␈↓ ↓H␈↓than␈αonce␈αin␈αthe␈αlist␈αand␈αthe␈αvalue␈αchosen␈αis␈αthat␈αpaired␈αwith␈αthe␈αfirst␈αoccurrence␈αof␈αthe␈αvariable.
␈↓ ↓H␈↓We illustrate this by the equation
␈↓ ↓H␈↓ ␈↓αeval␈↓↓[␈↓∧(CAR X), ((X.(B.C)) (Y.A) (X.B))␈↓↓] = ␈↓∧B␈↓,
␈↓ ↓H␈↓␈↓ ¬kCHAPTER IV␈↓ :68
␈↓ ↓H␈↓i.e.␈αwe␈αhave␈αevaluated␈α␈↓βa␈α␈↓αx␈↓␈α
with␈α␈↓α␈αx␈↓↓␈α=␈α␈↓∧(B.C)␈↓.␈α The␈αvalue␈α
associated␈αwith␈αa␈αvariable␈αin␈αsuch␈αa␈α
list␈αof
␈↓ ↓H␈↓pairs is computed by the auxiliary function ␈↓αassoc␈↓ which has the recursive definition
␈↓ ↓H␈↓ ␈↓αassoc␈↓↓[␈↓αv, a␈↓↓] ← ␈↓βif n ␈↓αa ␈↓βthen ␈↓∧NIL ␈↓βelse if aa ␈↓αa ␈↓β eq ␈↓αv ␈↓βthen a ␈↓αa ␈↓βelse ␈↓αalt␈↓↓[␈↓αv, ␈↓βd ␈↓αa␈↓↓]␈↓.
␈↓ ↓H␈↓Thus we have ␈↓αassoc␈↓↓[␈↓∧X, ((X.(B.C)) (Y.A) (X.B))␈↓↓] = ␈↓∧(X.(B.C))␈↓.
␈↓ ↓H␈↓ A simplified version of the usual LISP ␈↓αeval␈↓ is the following:
␈↓ ↓H␈↓␈↓αev→l␈↓↓[␈↓αe, a␈↓↓] ←
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βif at ␈↓αe ␈↓βthen ␈↓↓[␈↓βif ␈↓αnumberp e ␈↓↓∨ ␈↓αe ␈↓βeq ␈↓∧NIL ␈↓↓∨ ␈↓αe ␈↓βeq ␈↓∧T ␈↓βthen ␈↓αe ␈↓βelse␈↓α assoc␈↓↓[␈↓αe, a␈↓↓]]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if at a␈↓α e ␈↓βthen
␈↓ ↓H␈↓β␈↓ ␈↓β␈↓↓[␈↓βif a ␈↓αe␈↓β eq ␈↓∧CAR ␈↓βthen a ␈↓αeval␈↓↓[␈↓βad ␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe␈↓β eq ␈↓∧CDR ␈↓βthen d ␈↓αeval␈↓↓[␈↓βad ␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe␈↓β eq ␈↓∧CONS ␈↓βthen ␈↓αeval␈↓↓[␈↓βad ␈↓αe, a␈↓↓] . ␈↓αeval␈↓↓[␈↓βadd ␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe␈↓β eq ␈↓∧ATOM ␈↓βthen at ␈↓αeval␈↓↓[␈↓βad ␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe␈↓β eq ␈↓∧EQ ␈↓βthen at ␈↓αeval␈↓↓[␈↓βad ␈↓αe, a␈↓↓] ␈↓βeq ␈↓αeval␈↓↓[␈↓βadd ␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe␈↓β eq ␈↓∧QUOTE ␈↓βthen ad ␈↓αe
␈↓ ↓H␈↓α␈↓ ␈↓α␈↓βelse if a ␈↓αe␈↓β eq ␈↓∧COND ␈↓βthen ␈↓αevcon␈↓↓[␈↓βd ␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe␈↓β eq ␈↓∧LIST ␈↓βthen ␈↓αmapcar␈↓↓[␈↓βd ␈↓αe, λx␈↓↓: ␈↓αeval␈↓↓[␈↓αx, a␈↓↓]]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse ␈↓αeval␈↓↓[␈↓βd ␈↓αassoc␈↓↓[␈↓βa ␈↓αe, a␈↓↓] . ␈↓βd ␈↓αe, a␈↓↓]]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if aa ␈↓αe ␈↓βeq ␈↓∧LAMBDA ␈↓βthen ␈↓αeval␈↓↓[␈↓βadda ␈↓αe, prup␈↓↓[␈↓βada ␈↓αe, mapcar␈↓↓[␈↓βd ␈↓αe, ␈↓↓λ␈↓αx␈↓↓: ␈↓αeval␈↓↓[␈↓αx, a␈↓↓]]] * ␈↓αa␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if aa ␈↓αe ␈↓βeq ␈↓∧LABEL ␈↓βthen ␈↓αeval␈↓↓[␈↓βadda ␈↓αe . ␈↓βd ␈↓αe, ␈↓↓[␈↓βada ␈↓αe . ␈↓βa ␈↓αe␈↓↓] . ␈↓αa␈↓↓]␈↓,
␈↓ ↓H␈↓where the auxiliary function ␈↓αevcon␈↓ is defined by
␈↓ ↓H␈↓ ␈↓αevcon␈↓↓[␈↓αu, a␈↓↓] ← ␈↓βif ␈↓αeval␈↓↓[␈↓βaa ␈↓αu, a␈↓↓] ␈↓βthen ␈↓αeval␈↓↓[␈↓βada ␈↓αu, a␈↓↓] ␈↓βelse ␈↓αevcon␈↓↓[␈↓βd ␈↓αu, a␈↓↓]␈↓,
␈↓ ↓H␈↓and␈α∞the␈α
auxiliary␈α∞function␈α∞␈↓αprup␈↓␈α
used␈α∞for␈α∞pairing␈α
up␈α∞the␈α∞elements␈α
of␈α∞two␈α∞lists␈α
of␈α∞equal␈α∞length␈α
is
␈↓ ↓H␈↓defined by
␈↓ ↓H␈↓ ␈↓αprup␈↓↓[␈↓αu, v␈↓↓] ← ␈↓βif n ␈↓αu ␈↓βthen ␈↓∧NIL ␈↓βelse ␈↓↓[␈↓βa ␈↓αu . ␈↓βa ␈↓αv␈↓↓] . ␈↓αprup␈↓↓[␈↓βd ␈↓αu,␈↓βd ␈↓αv␈↓↓].␈↓
␈↓ ↓H␈↓ The␈α
way␈α
␈↓αeval␈↓␈α
works␈α
should␈α
be␈α
clear;␈α
atoms␈α
are␈α
either␈α
immediately␈α
evaluable␈α
or␈α
have␈α
to␈α
be
␈↓ ↓H␈↓looked␈α∩up␈α∩on␈α∩the␈α∩list␈α∩␈↓αa␈↓;␈α∩expressions␈α∩whose␈α∩first␈α∩term␈α∩is␈α∩one␈α∩of␈α∩the␈α∩elementary␈α∩functions␈α⊃are
␈↓ ↓H␈↓evaluated␈αby␈αperforming␈α
the␈αindicated␈αoperation␈αon␈α
the␈αresult␈αof␈α
evaluating␈αthe␈αarguments;␈α␈↓αlist␈↓␈α
has
␈↓ ↓H␈↓to␈α
be␈α
handled␈αspecially,␈α
because␈α
it␈αhas␈α
an␈α
indefinite␈α
number␈αof␈α
arguments;␈α
conditionals␈αare␈α
handled
␈↓ ↓H␈↓by␈α⊂an␈α⊂auxiliary␈α⊂function␈α⊂that␈α⊂evaluates␈α⊂the␈α⊂terms␈α⊂in␈α⊂the␈α⊂right␈α⊂order;␈α⊂quoted␈α⊂S-expressions␈α∂are
␈↓ ↓H␈↓trivial;␈α∞non-elementary␈α∞functions␈α∞have␈α
their␈α∞definitions␈α∞looked␈α∞up␈α
on␈α∞␈↓αa␈↓␈α∞and␈α∞substituted␈α∞for␈α
their
␈↓ ↓H␈↓names;␈α
when␈α
a␈αfunction␈α
is␈α
specified␈αby␈α
a␈α
λ,␈αthe␈α
inner␈α
expression␈αis␈α
evaluated␈α
with␈αa␈α
new␈α
␈↓αa␈↓␈αwhich␈α
is
␈↓ ↓H␈↓obtained␈α
by␈α
evaluating␈α
the␈α
arguments␈α
and␈α
pairing␈αthem␈α
up␈α
with␈α
the␈α
variables␈α
and␈α
putting␈αthem␈α
on
␈↓ ↓H␈↓the␈αfront␈α
of␈αthe␈α
old␈α␈↓αa␈↓;␈αand␈α
finally,␈α␈↓βlabel␈↓␈α
is␈αhandled␈α
by␈αpairing␈αthe␈α
name␈αof␈α
the␈αfunction␈α
with␈αthe
␈↓ ↓H␈↓expression on ␈↓αa␈↓ and replacing the whole function by the λ-part.
␈↓ ↓H␈↓ ␈↓αeval␈↓␈α
plays␈αboth␈α
a␈α
theoretical␈αand␈α
a␈αpractical␈α
role␈α
in␈αLISP.␈α
Historically,␈αthe␈α
list␈α
notation␈αfor
␈↓ ↓H␈↓LISP␈α
functions␈α
and␈α
␈↓αeval␈↓␈α
were␈α
first␈α
devised␈α
in␈α∞order␈α
to␈α
show␈α
how␈α
easy␈α
it␈α
is␈α
to␈α
define␈α∞a␈α
universal
␈↓ ↓H␈↓␈↓ ¬kCHAPTER IV␈↓ :69
␈↓ ↓H␈↓function␈αin␈αLISP␈α-␈αthe␈αidea␈αwas␈αto␈αadvocate␈αLISP␈αas␈αan␈αalternative␈αto␈αTuring␈αmachines␈αfor␈αdoing
␈↓ ↓H␈↓the␈α∞elementary␈α∞theory␈α
of␈α∞computability.␈α∞ The␈α
notation␈α∞used␈α∞was␈α
chosen␈α∞without␈α∞much␈α∞regard␈α
for
␈↓ ↓H␈↓human␈α
convenience,␈α
because␈α
the␈α
original␈α∞idea␈α
was␈α
purely␈α
theoretical;␈α
the␈α
notation␈α∞for␈α
conditional
␈↓ ↓H␈↓expressions,␈α∞for␈α∞example,␈α∂has␈α∞an␈α∞unnecessary␈α∂extra␈α∞level␈α∞of␈α∂parentheses.␈α∞ However,␈α∞S.␈α∂R.␈α∞Russell
␈↓ ↓H␈↓noted␈α∞that␈α∞␈↓αeval␈↓␈α∞could␈α∞serve␈α∞as␈α∞an␈α
interpreter␈α∞for␈α∞LISP␈α∞and␈α∞promptly␈α∞programmed␈α∞it␈α∞in␈α
machine
␈↓ ↓H␈↓language␈αwith␈αminor␈α
modifications␈αfor␈αpractical␈α
purposes.␈α Since␈αa␈α
compiler␈αwas␈αlong␈α
delayed,␈αthe
␈↓ ↓H␈↓interpreter␈α∞was␈α
more␈α∞easily␈α
modified␈α∞and␈α
handled␈α∞some␈α
difficult␈α∞cases␈α
with␈α∞functional␈α
arguments
␈↓ ↓H␈↓better, an interpreter based on ␈↓αeval␈↓ has remained a feature of most LISP systems.
␈↓ ↓H␈↓ The␈α⊃way␈α⊃␈↓αeval␈↓␈α∩handles␈α⊃arguments␈α⊃corresponds␈α⊃to␈α∩the␈α⊃call-by-value␈α⊃method␈α∩of␈α⊃parameter
␈↓ ↓H␈↓passing␈αin␈αALGOL␈α
and␈αsimilar␈αlanguages.␈α
There␈αis␈αalso␈α
a␈αform␈αof␈α
␈↓αeval␈↓␈αthat␈αcorresponds␈α
to␈αcall-
␈↓ ↓H␈↓by-name. Here it is:
␈↓ ↓H␈↓ ␈↓αneval␈↓↓[␈↓αe, a␈↓↓] ← ␈↓βif at ␈↓αe␈↓β then
␈↓ ↓H␈↓β␈↓ ␈↓β␈↓↓[␈↓βif ␈↓αe ␈↓βeq ␈↓∧T ␈↓β then ␈↓∧T
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓βelse if ␈↓αe ␈↓βeq ␈↓∧NIL ␈↓βthen ␈↓∧NIL
␈↓ ↓H␈↓∧␈↓ ␈↓∧␈↓βelse if ␈↓αnumberp e ␈↓βthen ␈↓αe
␈↓ ↓H␈↓α␈↓ ␈↓α␈↓βelse ␈↓αneval␈↓↓[␈↓βad ␈↓αassoc␈↓↓[␈↓αe, a␈↓↓], ␈↓βdd ␈↓α assoc␈↓↓[␈↓αe, a␈↓↓]]]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if at a ␈↓αe ␈↓β then
␈↓ ↓H␈↓β␈↓ ␈↓β␈↓↓[␈↓βif a ␈↓αe ␈↓βeq ␈↓∧CAR ␈↓βthen a ␈↓αneval␈↓↓[␈↓βad ␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe ␈↓βeq ␈↓∧CDR ␈↓βthen d ␈↓αneval␈↓↓[␈↓βad ␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe ␈↓βeq ␈↓∧CONS ␈↓βthen ␈↓αneval␈↓↓[␈↓βad ␈↓αe, a␈↓↓] . neval[␈↓βadd ␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe ␈↓βeq ␈↓∧ATOM ␈↓βthen at ␈↓αneval␈↓↓[␈↓βad ␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe ␈↓βeq ␈↓∧EQ ␈↓βthen ␈↓αneval␈↓↓[␈↓βad ␈↓αe, a␈↓↓] ␈↓βeq ␈↓αneval␈↓↓[␈↓βadd ␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe ␈↓βeq ␈↓∧QUOTE ␈↓βthen ad ␈↓αe
␈↓ ↓H␈↓α␈↓ ␈↓α␈↓βelse if a ␈↓αe ␈↓βeq ␈↓∧COND ␈↓βthen ␈↓αnevcon␈↓↓[␈↓βd ␈↓αe, a␈↓↓]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if a ␈↓αe ␈↓βeq ␈↓∧LIST ␈↓βthen ␈↓αmapcar␈↓↓[␈↓βd ␈↓αe ␈↓↓, λ␈↓αx␈↓↓: ␈↓αneval␈↓↓[␈↓αx, a␈↓↓]]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse ␈↓αneval␈↓↓[␈↓βd ␈↓αassoc␈↓↓[␈↓βa ␈↓αe, a␈↓↓] . ␈↓βd ␈↓αe, a␈↓↓]]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if aa ␈↓αe ␈↓βeq ␈↓∧LAMBDA ␈↓βthen ␈↓αneval␈↓↓[␈↓βadda ␈↓αe, nprup␈↓↓[␈↓βada ␈↓αe, ␈↓βd ␈↓αe␈↓↓, ␈↓αa␈↓↓]]
␈↓ ↓H␈↓↓␈↓ ␈↓↓␈↓βelse if aa ␈↓αe ␈↓βeq ␈↓∧LABEL ␈↓βthen ␈↓αneval␈↓↓[␈↓βadda ␈↓αe . ␈↓βd ␈↓αe, ␈↓↓[␈↓βada ␈↓αe . ␈↓βa ␈↓αe␈↓↓] . ␈↓αa␈↓↓], ␈↓
␈↓ ↓H␈↓where the auxiliary function ␈↓αnevcon␈↓ is given by
␈↓ ↓H␈↓ ␈↓αnevcon␈↓↓[␈↓αu, a␈↓↓] ← ␈↓βif ␈↓αneval␈↓↓[␈↓βaa ␈↓αu, a␈↓↓] ␈↓βthen ␈↓αneval␈↓↓[␈↓βada ␈↓αu, a␈↓↓] ␈↓βelse ␈↓αnevcon␈↓↓[␈↓βd ␈↓αu, a␈↓↓].␈↓
␈↓ ↓H␈↓and nprup is
␈↓ ↓H␈↓ ␈↓αnprup␈↓↓[␈↓αu, v,a␈↓↓] ← ␈↓βif n ␈↓αu ␈↓βthen ␈↓αa ␈↓βelse ␈↓↓[␈↓βa ␈↓αu . ␈↓↓[␈↓βa ␈↓αv . a␈↓↓]] . ␈↓αnprup␈↓↓[␈↓βd ␈↓αu, ␈↓βd ␈↓αv,a␈↓↓].␈↓
␈↓ ↓H␈↓ The␈α∞difference␈α∂between␈α∞␈↓αeval␈↓␈α∞and␈α∂␈↓αneval␈↓␈α∞is␈α∞only␈α∂in␈α∞two␈α∞terms.␈α∂ ␈↓αeval␈↓␈α∞evaluates␈α∞a␈α∂variable␈α∞by
␈↓ ↓H␈↓looking␈αit␈αup␈αon␈αthe␈αassociation␈αlist␈αwhereas␈α␈↓αneval␈↓␈αlooks␈αit␈αup␈αon␈αthe␈αassociation␈αlist␈αand␈αevaluates
␈↓ ↓H␈↓the␈α
result␈α
in␈α
the␈α
context␈α
in␈α
which␈α
it␈αwas␈α
put␈α
on␈α
the␈α
association␈α
list.␈α
Correspondingly,␈α
when␈α
a␈αλ-
␈↓ ↓H␈↓expression␈αis␈αencountered,␈α␈↓αeval␈↓␈αforms␈αa␈αnew␈αassociation␈αlist␈αby␈αpairing␈αthe␈αvalues␈αof␈αthe␈αarguments
␈↓ ↓H␈↓with␈α
the␈α
variables␈α
bound␈α∞by␈α
the␈α
λ␈α
and␈α∞putting␈α
the␈α
new␈α
pairs␈α
in␈α∞front␈α
of␈α
the␈α
old␈α∞association␈α
list,
␈↓ ↓H␈↓whereas␈α␈↓αneval␈↓␈α
pairs␈αthe␈α
arguments␈αthemselves␈α
with␈αthe␈αvariables␈α
and␈αputs␈α
them␈αon␈α
the␈αfront␈αof␈α
the
␈↓ ↓H␈↓␈↓ ¬kCHAPTER IV␈↓ :70
␈↓ ↓H␈↓association␈αlist.␈α
The␈αfunction␈α
neval␈αalso␈α
saves␈αthe␈α
current␈αassociation␈α
list␈αwith␈α
each␈αvariable␈αon␈α
the
␈↓ ↓H␈↓association␈α∞list,␈α
so␈α∞that␈α
the␈α∞variables␈α
can␈α∞be␈α
evaluated␈α∞in␈α
the␈α∞correct␈α
context.␈α∞ In␈α
most␈α∞cases␈α
both
␈↓ ↓H␈↓give␈αthe␈α
same␈αresult␈αwith␈α
the␈αsame␈αwork,␈α
but␈α␈↓αneval␈↓␈α
gives␈αa␈αresult␈α
in␈αsome␈αcases␈α
in␈αwhich␈α␈↓αeval␈↓␈α
loops.
␈↓ ↓H␈↓An example is obtained by evaluating ␈↓αF␈↓↓[2, 1]␈↓ where ␈↓αF␈↓ is defined by
␈↓ ↓H␈↓ ␈↓αF␈↓↓[␈↓αx, y␈↓↓] ← ␈↓βif␈↓α x␈↓↓=0 ␈↓βthen ␈↓↓0 ␈↓βelse ␈↓αF␈↓↓[␈↓αx␈↓↓-1, ␈↓αF␈↓↓[␈↓αy␈↓↓-2, ␈↓αx␈↓↓]].␈↓
␈↓ ↓H␈↓Exercises
␈↓ ↓H␈↓ 1.␈αWrite␈α
␈↓αneval␈↓␈αand␈αthe␈α
necessary␈αauxiliary␈α
functions␈αin␈αlist␈α
form,␈αand␈αtry␈α
them␈αout␈α
on␈αsome
␈↓ ↓H␈↓examples.
␈↓ ↓H␈↓2. ␈↓βComputability.␈↓
␈↓ ↓H␈↓ Some␈α∂LISP␈α∂calculations␈α∂run␈α∞on␈α∂indefinitely.␈α∂ The␈α∂most␈α∂trivial␈α∞case␈α∂occurs␈α∂if␈α∂we␈α∂make␈α∞the
␈↓ ↓H␈↓recursive definition
␈↓ ↓H␈↓ ␈↓αloop x ␈↓↓← ␈↓αloop x␈↓
␈↓ ↓H␈↓and␈αattempt␈αto␈αcompute␈α␈↓αloop␈↓↓[␈↓αx␈↓↓]␈↓␈αfor␈αany␈α␈↓αx␈↓␈αwhatsoever.␈α Don't␈αdismiss␈αthis␈αexample␈αjust␈αbecause␈αno-
␈↓ ↓H␈↓one␈αwould␈αwrite␈αsuch␈αan␈αobviously␈αuseless␈αfunction␈αdefinition.␈α There␈αis␈αa␈αsense␈αin␈αwhich␈αit␈αis␈αthe
␈↓ ↓H␈↓"zero"␈αof␈α
a␈αlarge␈α
class␈αof␈αnon¬terminating␈α
function␈αdefinitions,␈α
and,␈αas␈α
the␈αRomans␈αexperienced␈α
but
␈↓ ↓H␈↓never learned, leaving zero out of the number system is a mistake.
␈↓ ↓H␈↓ Nevertheless,␈αin␈αmost␈αprogramming,␈αnon-terminating␈α
calculations␈αare␈αthe␈αresults␈αof␈α
errors␈αin
␈↓ ↓H␈↓defining␈α
functions.␈α
Therefore,␈α
it␈α
would␈α
be␈α
useful␈α
to␈α
be␈α
able␈α
to␈α
tell␈α
whether␈α
a␈α
function␈αdefinition
␈↓ ↓H␈↓gives␈α
a␈α
result␈α
for␈α
all␈α
arguments.␈α In␈α
fact,␈α
it␈α
would␈α
be␈α
useful␈α
to␈αbe␈α
able␈α
to␈α
tell␈α
whether␈α
a␈αfunction␈α
will
␈↓ ↓H␈↓terminate for a single argument. Let us make this goal more precise.
␈↓ ↓H␈↓ Suppose␈α∂that␈α∞␈↓αf␈↓␈α∂is␈α∞a␈α∂LISP␈α∂function␈α∞and␈α∂␈↓αa␈↓␈α∞is␈α∂an␈α∂S-expression,␈α∞and␈α∂we␈α∞would␈α∂like␈α∂to␈α∞know
␈↓ ↓H␈↓whether␈α
the␈αcomputation␈α
of␈α␈↓αf␈↓↓[␈↓αa␈↓↓]␈↓␈α
terminates.␈α
Suppose␈α␈↓αf␈↓␈α
is␈αrepresented␈α
by␈α
the␈αS-expression␈α
␈↓αf*␈↓␈αin␈α
the
␈↓ ↓H␈↓usual␈α↔S-expression␈α⊗notation␈α↔for␈α⊗LISP␈α↔functions.␈α⊗ Then␈α↔the␈α⊗S-expression␈α↔␈↓∧(f*␈α↔(QUOTE␈α⊗a))␈↓
␈↓ ↓H␈↓represents␈α␈↓αf␈↓↓[␈↓αa␈↓↓]␈↓.␈α
Define␈αthe␈α
function␈α␈↓αterm␈↓␈α
by␈αgiving␈α
␈↓αterm␈↓↓[␈↓αe␈↓↓]␈↓␈αthe␈α
value␈α␈↓βtrue␈↓α␈α
if␈αe␈↓␈α
is␈αan␈αS-expression␈α
of
␈↓ ↓H␈↓the␈α
form␈α
␈↓∧(f*␈α
(QUOTE␈α
a))␈↓␈α
for␈α
which␈α␈↓αf␈↓↓[␈↓αa␈↓↓]␈↓␈α
terminates␈α
and␈α
␈↓βfalse␈↓␈α
otherwise.␈α
We␈α
now␈α
ask␈αwhether␈α
␈↓αterm␈↓
␈↓ ↓H␈↓is␈α
a␈αLISP␈α
function,␈αi.e.␈α
can␈αit␈α
be␈αconstructed␈α
from␈α
␈↓αcar,␈αcdr,␈α
cons,␈αatom,␈α
␈↓␈αand␈α
␈↓αeq␈↓␈αusing␈α
λ,␈α␈↓βlabel␈↓,␈α
and
␈↓ ↓H␈↓conditional␈α∂expressions?␈α∂ Well,␈α⊂it␈α∂can't,␈α∂as␈α∂we␈α⊂shall␈α∂shortly␈α∂prove,␈α∂and␈α⊂this␈α∂means␈α∂that␈α∂it␈α⊂is␈α∂not
␈↓ ↓H␈↓␈↓αcomputable␈↓␈αwhether␈αa␈αLISP␈αcalculation␈αterminates,␈αsince␈αif␈α␈↓αterm␈↓␈αwere␈αcomputable␈αby␈αany␈αcomputer
␈↓ ↓H␈↓or in any recognized sense, it could be represented as a LISP function. Here is the proof:
␈↓ ↓H␈↓ Consider the function ␈↓αterma␈↓ defined from ␈↓αterm␈↓ by
␈↓ ↓H␈↓ ␈↓αterma u ␈↓↓← ␈↓βif␈↓α term list␈↓↓[␈↓αu, list␈↓↓[␈↓∧QUOTE␈↓α, u␈↓↓]] ␈↓βthen␈↓α loop u ␈↓βelse true␈↓,
␈↓ ↓H␈↓and␈α∂suppose␈α∂that␈α∂␈↓αf␈↓␈α∂is␈α∂a␈α∂LISP␈α∂function␈α∞and␈α∂that␈α∂␈↓αf*␈↓␈α∂is␈α∂its␈α∂S-expression␈α∂representation.␈α∂ What␈α∞is
␈↓ ↓H␈↓␈↓ ¬kCHAPTER IV␈↓ :71
␈↓ ↓H␈↓␈↓αterma␈αf*␈↓?␈α
Well␈α␈↓αterma␈α
f*␈↓␈αtells␈α
us␈αwhether␈αthe␈α
computation␈αof␈α
␈↓αf␈↓↓[␈↓αf*␈↓↓]␈↓␈αterminates,␈α
and␈αit␈α
tells␈αus␈αthis␈α
by
␈↓ ↓H␈↓going␈αinto␈αa␈α
loop␈αif␈α␈↓αf␈↓↓[␈↓αf*␈↓↓]␈↓␈α
terminates␈αand␈αgiving␈α␈↓βtrue␈↓␈α
otherwise.␈α Now␈αif␈α
␈↓αterm␈↓␈αwere␈αa␈αLISP␈α
function,
␈↓ ↓H␈↓then␈α
␈↓αterma␈↓␈α
would␈α
also␈α∞be␈α
a␈α
LISP␈α
function.␈α
Indeed␈α∞if␈α
␈↓αterm␈↓␈α
were␈α
represented␈α
by␈α∞the␈α
S-expression
␈↓ ↓H␈↓␈↓αterm*␈↓, then ␈↓αterma␈↓ would be represented by the S-expression
␈↓ ↓H␈↓ ␈↓αterma*␈α
␈↓∧=␈α
(LAMBDA␈α
(U)␈α
(COND␈α
((␈↓αterm*␈↓∧␈α
(LIST␈α
U␈α
(LIST␈α
(QUOTE␈α
QUOTE)␈α
U)))␈α
(LOOP␈α
U))
␈↓ ↓H␈↓∧(T T))).
␈↓ ↓H␈↓∧Now␈α∪consider␈α∪␈↓αterma␈↓↓[␈↓αterma*␈↓↓]␈↓.␈α∪ According␈α∪to␈α∪the␈α∪definition␈α∪of␈α∪␈↓αterma␈↓,␈α∪this␈α∪will␈α∪tell␈α∪us␈α∩whether
␈↓ ↓H␈↓␈↓αterma␈↓↓[␈↓αterma*␈↓↓]␈↓␈α
is␈α
defined,␈α
i.e.␈α
it␈α
tells␈α
about␈α
itself.␈α
However,␈α
it␈α
gives␈α
this␈α
answer␈α
in␈α
a␈α
contradictory
␈↓ ↓H␈↓way;␈αnamely␈α
␈↓αterma␈↓↓[␈↓αterma*␈↓↓]␈↓␈αlooping␈αtells␈α
us␈αthat␈α␈↓αterma␈↓↓[␈↓αterma*␈↓↓]␈↓␈α
terminates,␈αand␈α
␈↓αterma␈↓↓[␈↓αterma*␈↓↓]␈↓␈αbeing
␈↓ ↓H␈↓␈↓βtrue␈↓␈α∂tells␈α∂us␈α∂that␈α∂␈↓αterma␈↓↓[␈↓αterma*␈↓↓]␈↓␈α∂doesn't␈α∂terminate.␈α∂ This␈α∂contradiction␈α∂tells␈α∂us␈α∂that␈α∂␈↓αterm␈↓␈α∂is␈α∂not␈α∞a
␈↓ ↓H␈↓LISP function, and there is no general procedure for telling whether a LISP calculation terminates.
␈↓ ↓H␈↓ The␈α∪above␈α∪result␈α∪does␈α∪not␈α∪exclude␈α∩LISP␈α∪functions␈α∪that␈α∪tell␈α∪whether␈α∪LISP␈α∩calculations
␈↓ ↓H␈↓terminate.␈α∩ It␈α∪just␈α∩excludes␈α∩perfect␈α∪ones.␈α∩ Suppose␈α∩we␈α∪have␈α∩a␈α∩function␈α∪␈↓αt␈↓␈α∩that␈α∪sometimes␈α∩says
␈↓ ↓H␈↓calculations␈α
terminate,␈α
sometimes␈αsays␈α
they␈α
don't␈α
terminate,␈αand␈α
sometimes␈α
runs␈α
on␈αindefinitely.␈α
We
␈↓ ↓H␈↓shall␈αfurther␈αassume␈αthat␈αwhen␈α␈↓αt␈↓␈αgives␈αan␈αanswer␈αit␈αis␈αalways␈αright.␈α Given␈αsuch␈αa␈αfunction␈αwe␈αcan
␈↓ ↓H␈↓improve␈αit␈αa␈αbit␈αso␈αthat␈αit␈αwill␈αalways␈αgive␈αthe␈αright␈αanswer␈αwhen␈αthe␈αcalculation␈αit␈αis␈αasked␈αabout
␈↓ ↓H␈↓terminates.␈α∂ This␈α∞is␈α∂done␈α∞by␈α∂mixing␈α∞the␈α∂computation␈α∞of␈α∂␈↓αt␈↓↓[␈↓αe␈↓↓]␈↓␈α∞with␈α∂a␈α∞computation␈α∂of␈α∂␈↓αeval␈↓↓[␈↓αe,␈α∞␈↓∧NIL␈↓↓]␈↓
␈↓ ↓H␈↓doing␈αthe␈αcomputations␈αalternately.␈α
If␈αthe␈α␈↓αeval␈↓↓[␈↓αe,␈α␈↓∧NIL␈↓↓]␈↓␈α
computation␈αever␈αterminates,␈αthen␈α
the␈αnew
␈↓ ↓H␈↓function asserts termination.
␈↓ ↓H␈↓ Given␈αsuch␈αa␈α␈↓αt␈↓,␈αwe␈αcan␈αalways␈αfind␈αa␈αcalculation␈αthat␈αdoes␈αnot␈αterminate␈αbut␈α␈↓αt␈↓␈αdoesn't␈αsay␈α
so.
␈↓ ↓H␈↓The construction is just like that used in the previous proof. Given ␈↓αt␈↓, we construct
␈↓ ↓H␈↓ ␈↓αta u ␈↓↓← ␈↓βif␈↓α t list␈↓↓[␈↓αu, list␈↓↓[␈↓∧QUOTE␈↓α, u␈↓↓]] ␈↓βthen␈↓α loop u ␈↓βelse true␈↓,
␈↓ ↓H␈↓and␈αthen␈αwe␈αconsider␈α␈↓αta␈↓↓[␈↓αta*␈↓↓]␈↓.␈α If␈αthis␈αhad␈αthe␈αvalue␈α␈↓βtrue␈↓,␈αthen␈αit␈αwouldn't␈αterminate␈αso␈αtherefore␈αit
␈↓ ↓H␈↓doesn't␈αterminate␈αbut␈αis␈αnot␈α
one␈αof␈αthose␈αexpressions␈αwhich␈α
␈↓αt␈↓␈αdecides.␈α Thus␈αfor␈αany␈αpartial␈α
decider
␈↓ ↓H␈↓we can find a LISP calculation which doesn't terminate but which the decider doesn't decide.
␈↓ ↓H␈↓ This can in turn be used to get a slightly better decider, namely
␈↓ ↓H␈↓ ␈↓αt␈↓¬1␈↓↓[␈↓αe␈↓↓] ← ␈↓βif␈↓α e ␈↓↓= ␈↓αta* ␈↓βthen ␈↓∧DOESN'T-TERMINATE ␈↓βelse␈↓α t␈↓↓[␈↓αe␈↓↓]␈↓.
␈↓ ↓H␈↓Of␈α
course,␈α␈↓αt␈↓¬1␈↓␈α
isn't␈αmuch␈α
better␈αthan␈α
␈↓αt␈↓,␈αsince␈α
it␈αcan␈α
decide␈αonly␈α
one␈αmore␈α
computation,␈αbut␈α
we␈αcan
␈↓ ↓H␈↓form␈α␈↓αt␈↓¬2␈↓␈αby␈α
applying␈αthe␈αsame␈α
process,␈αand␈αso␈α
forth.␈α In␈αfact,␈α
we␈αcan␈αeven␈α
form␈α␈↓αt␈↓¬∞␈↓␈αwhich␈αdecides␈α
all
␈↓ ↓H␈↓the␈αcases␈αdecided␈αby␈αany␈α␈↓αt␈↓¬n␈↓.␈α This␈αcan␈αbe␈αfurther␈αimproved␈αby␈αthe␈αsame␈αprocess,␈αetc.␈α How␈αfar␈αcan
␈↓ ↓H␈↓we␈αgo?␈α The␈αanswer␈αis␈αtechnical;␈αnamely,␈αthe␈αimprovement␈αprocess␈αcan␈αbe␈αcarried␈αout␈αany␈α
recursive
␈↓ ↓H␈↓ordinal number of times.
␈↓ ↓H␈↓ Unfortunately,␈α⊃this␈α⊃kind␈α⊃of␈α⊃improvement␈α⊃seems␈α⊃to␈α⊃be␈α⊃superficial,␈α⊃since␈α⊃none␈α⊃of␈α⊃the␈α⊂new
␈↓ ↓H␈↓computations proved not to terminate are likely to be of practical interest.
␈↓ ↓H␈↓Exercises.
␈↓ ↓H␈↓␈↓ ¬kCHAPTER IV␈↓ :72
␈↓ ↓H␈↓1. Write a function that gives ␈↓αt␈↓¬n+1␈↓ in terms of ␈↓αt␈↓¬n␈↓.
␈↓ ↓H␈↓2. Write a function that gives ␈↓αt␈↓¬∞␈↓ in terms of ␈↓αt␈↓.
␈↓ ↓H␈↓3.␈α∂If␈α∂you␈α∂know␈α∂about␈α∂Turing␈α∂machines,␈α∞write␈α∂a␈α∂LISP␈α∂function␈α∂to␈α∂simulate␈α∂an␈α∂arbitrary␈α∞Turing
␈↓ ↓H␈↓machine given a description of the machine in some convenient notation.
␈↓ ↓H␈↓4.␈α∞Write␈α∞a␈α∞LISP␈α∞function␈α∞that␈α∞will␈α
translate␈α∞a␈α∞Turing␈α∞machine␈α∞description␈α∞into␈α∞a␈α∞LISP␈α
function
␈↓ ↓H␈↓that will do the same computation.
␈↓ ↓H␈↓5.␈α
If␈α
you␈αreally␈α
like␈α
Turing␈αmachines,␈α
write␈α
a␈αdescription␈α
of␈α
a␈αTuring␈α
machine␈α
that␈α
will␈αinterpret
␈↓ ↓H␈↓LISP internal notation.
␈↓ ↓H␈↓␈↓ εP␈↓ :73
␈↓ ↓H␈↓β␈↓ ¬tCHAPTER V
␈↓ ↓H␈↓β␈↓ ∧0PROVING LISP PROGRAMS CORRECT
␈↓ ↓H␈↓ In␈α∪this␈α∪chapter␈α∩we␈α∪will␈α∪introduce␈α∩techniques␈α∪for␈α∪proving␈α∩LISP␈α∪programs␈α∪correct.␈α∩ The
␈↓ ↓H␈↓techniques␈α
will␈α
mainly␈α
be␈α
limited␈α
to␈α
what␈α∞we␈α
may␈α
call␈α
␈↓αclean␈↓␈α
LISP␈α
programs.␈α
In␈α∞particular,␈α
there
␈↓ ↓H␈↓must␈α∞be␈α∞no␈α
side␈α∞effects,␈α∞because␈α
our␈α∞methods␈α∞depend␈α
on␈α∞the␈α∞ability␈α
to␈α∞replace␈α∞subexpressions␈α
by
␈↓ ↓H␈↓equal expressions.
␈↓ ↓H␈↓ The␈α
necessary␈α
basic␈α
facts␈α
can␈α
be␈α
divided␈α
into␈α
several␈α
categories:␈α
first␈α
order␈α
logic␈α
including
␈↓ ↓H␈↓conditional␈αforms␈αand␈αfirst␈αorder␈αlambda-expressions,␈αalgebraic␈αfacts␈αabout␈αlists␈αand␈αS-expressions,
␈↓ ↓H␈↓facts␈α∂about␈α∂the␈α∂inductive␈α∂structure␈α∂of␈α∂lists␈α∂and␈α∂S-expressions,␈α∂and␈α∂general␈α∂facts␈α⊂about␈α∂functions
␈↓ ↓H␈↓defined␈α⊃by␈α⊃recursion.␈α⊂ Ideally,␈α⊃we␈α⊃would␈α⊃use␈α⊂a␈α⊃general␈α⊃theory␈α⊂of␈α⊃recursive␈α⊃definition␈α⊃to␈α⊂prove
␈↓ ↓H␈↓theorems␈αabout␈αLISP␈αfunctions.␈α However,␈αthe␈αgeneral␈αtheory␈αis␈αnot␈αwell␈αenough␈αdeveloped,␈αso␈αwe
␈↓ ↓H␈↓have␈α⊂had␈α∂to␈α⊂introduce␈α⊂some␈α∂methods␈α⊂limited␈α⊂to␈α∂LISP␈α⊂functions␈α⊂defined␈α∂by␈α⊂particular␈α⊂kinds␈α∂of
␈↓ ↓H␈↓recursion schemes.
␈↓ ↓H␈↓1. ␈↓βFirst order logic with conditional forms and lambda-expressions.␈↓
␈↓ ↓H␈↓ The␈α⊂logic␈α⊂we␈α⊂shall␈α⊂use␈α⊂is␈α⊂called␈α⊂first␈α⊂order␈α⊂logic␈α⊂with␈α⊂equality,␈α⊂but␈α⊂we␈α⊂will␈α⊂extend␈α⊂it␈α⊂by
␈↓ ↓H␈↓allowing␈α
conditional␈α
forms␈αto␈α
be␈α
terms␈αand␈α
lambda-expressions␈α
to␈αbe␈α
function␈α
expressions.␈α From
␈↓ ↓H␈↓the␈α∂mathematical␈α⊂point␈α∂of␈α⊂view,␈α∂these␈α⊂extensions␈α∂are␈α∂inessential,␈α⊂because,␈α∂as␈α⊂we␈α∂shall␈α⊂see,␈α∂every
␈↓ ↓H␈↓sentence␈αthat␈αincludes␈αconditional␈αforms␈αor␈αfirst␈αorder␈αlambdas␈αcan␈αreadily␈αbe␈αtransformed␈αinto␈αan
␈↓ ↓H␈↓equivalent␈αsentence␈αwithout␈αthem.␈α However,␈αthe␈αextensions␈αare␈αpractically␈αimportant,␈αbecause␈αthey
␈↓ ↓H␈↓permit␈α
us␈α
to␈α
use␈α
certain␈α
recursive␈α∞definitions␈α
directly␈α
as␈α
formulas␈α
of␈α
the␈α
logic.␈α∞ Unfortunately,␈α
we
␈↓ ↓H␈↓will␈α
not␈αbe␈α
able␈α
to␈αtreat␈α
all␈α
recursive␈αdefinitions␈α
in␈αthis␈α
system,␈α
but␈αonly␈α
those␈α
which␈αwe␈α
can␈αbe␈α
sure
␈↓ ↓H␈↓are␈α∂defined␈α∞for␈α∂all␈α∞values␈α∂of␈α∞the␈α∂arguments.␈α∞ Moreover,␈α∂we␈α∞will␈α∂be␈α∞unable␈α∂to␈α∂prove␈α∞definedness
␈↓ ↓H␈↓within␈α∞the␈α
system,␈α∞so␈α∞we␈α
will␈α∞be␈α∞restricted␈α
to␈α∞classes␈α
of␈α∞recursive␈α∞definitions␈α
which␈α∞we␈α∞can␈α
prove
␈↓ ↓H␈↓always␈α
defined␈α
by␈α
an␈α
argument␈α∞outside␈α
the␈α
system.␈α
Fortunately,␈α
many␈α
interesting␈α∞LISP␈α
functions
␈↓ ↓H␈↓meet␈α∂these␈α∂restrictions␈α∂including␈α⊂all␈α∂the␈α∂important␈α∂functions␈α∂so␈α⊂far␈α∂used␈α∂except␈α∂for␈α∂␈↓αeval␈↓␈α⊂and␈α∂its
␈↓ ↓H␈↓variants.
␈↓ ↓H␈↓ Formulas␈α⊂of␈α⊂the␈α⊂logic␈α⊂are␈α⊂built␈α⊂from␈α⊂constants,␈α⊂variables␈α⊂predicate␈α⊂symbols,␈α⊂and␈α∂function
␈↓ ↓H␈↓symbols␈α⊃using␈α⊃function␈α∩application,␈α⊃conditional␈α⊃forms,␈α∩boolean␈α⊃forms,␈α⊃lambda␈α∩expressions,␈α⊃and
␈↓ ↓H␈↓quantifiers.
␈↓ ↓H␈↓␈↓βConstants␈↓:␈α∂We␈α∂will␈α∂use␈α∂S-expresssions␈α∂as␈α∞constants␈α∂standing␈α∂for␈α∂themselves␈α∂and␈α∂also␈α∂lower␈α∞case
␈↓ ↓H␈↓letters␈α∪from␈α∪the␈α∪first␈α∀part␈α∪of␈α∪the␈α∪alphabet␈α∪to␈α∀represent␈α∪constants␈α∪in␈α∪other␈α∪domains␈α∀than␈α∪S-
␈↓ ↓H␈↓expressions.
␈↓ ↓H␈↓␈↓βVariables␈↓: We will use the letters ␈↓αu␈↓ thru ␈↓αz␈↓ with or without subscripts as variables.
␈↓ ↓H␈↓␈↓ ¬pCHAPTER V␈↓ :74
␈↓ ↓H␈↓␈↓βFunction␈αsymbols␈↓:␈αThe␈αletters␈α␈↓αf␈↓,␈α␈↓αg␈↓␈αand␈α␈↓αh␈↓␈αwith␈αor␈αwithout␈αsubscripts␈αare␈αused␈αas␈αfunction␈αsymbols.
␈↓ ↓H␈↓The␈αLISP␈αfunction␈αsymbols␈α␈↓βa␈↓,␈α␈↓βd␈↓␈α
and␈α.␈α(as␈αan␈αinfix)␈αare␈α
also␈αused␈αas␈αfunction␈αsymbols.␈α We␈α
suppose
␈↓ ↓H␈↓that each function symbol takes the same definite number of arguments every time it is used.
␈↓ ↓H␈↓␈↓βPredicate␈αsymbols␈↓:␈αThe␈αletters␈α␈↓αp␈↓,␈α␈↓αq␈↓␈αand␈α␈↓αr␈↓␈αwith␈αor␈αwithout␈αsubscripts␈αare␈αused␈αas␈αpredicate␈α
symbols.
␈↓ ↓H␈↓We␈α
will␈α
also␈α
use␈α
the␈αLISP␈α
predicate␈α
symbol␈α
␈↓βat␈↓␈α
as␈αa␈α
constant␈α
predicate␈α
symbol.␈α
The␈αequality␈α
symbol
␈↓ ↓H␈↓=␈αis␈αalso␈αused␈αas␈αan␈αinfix.␈α We␈αsuppose␈αthat␈αeach␈αpredicate␈αsymbol␈αtakes␈αthe␈αsame␈αdefinite␈αnumber
␈↓ ↓H␈↓of arguments each time it is used.
␈↓ ↓H␈↓ Next␈α
we␈α
define␈α
terms,␈α
sentences,␈α
function␈α
expressions␈α
and␈α
predicate␈α
expressions␈αinductively.
␈↓ ↓H␈↓A␈αterm␈α
is␈αan␈α
expression␈αwhose␈α
value␈αwill␈α
be␈αan␈αobject␈α
like␈αan␈α
S-expression␈αor␈α
a␈αnumber␈α
while␈αa
␈↓ ↓H␈↓sentence␈α
has␈α
value␈α
␈↓∧T␈↓␈α
or␈α␈↓∧F␈↓.␈α
Terms␈α
are␈α
used␈α
in␈α
making␈αsentences,␈α
and␈α
sentences␈α
occur␈α
in␈α
terms␈αso
␈↓ ↓H␈↓that␈α∀the␈α∀definitions␈α∀are␈α∀␈↓αmutually␈α∀recursive␈↓␈α∃where␈α∀this␈α∀use␈α∀of␈α∀the␈α∀word␈α∀␈↓αrecursive␈↓␈α∃should␈α∀be
␈↓ ↓H␈↓distinguished␈α⊃from␈α⊃its␈α⊃use␈α⊃in␈α⊃recursive␈α⊃definitions␈α⊃of␈α⊃functions.␈α⊃ Function␈α⊃expressions␈α⊃are␈α⊃also
␈↓ ↓H␈↓involved in the mutual recursion.
␈↓ ↓H␈↓␈↓βTerms␈↓:␈α∩Constants␈α⊃are␈α∩terms,␈α⊃and␈α∩variables␈α⊃are␈α∩terms.␈α⊃ If␈α∩␈↓αf␈↓␈α⊃is␈α∩a␈α⊃function␈α∩expression␈α∩taking␈α⊃␈↓αn␈↓
␈↓ ↓H␈↓arguments,␈αand␈α␈↓αt␈↓¬1␈↓α, ... ,t␈↓¬n␈↓α␈↓␈αare␈αterms,␈αthen␈α␈↓αf[t␈↓¬1␈↓α, ... ,t␈↓¬n␈↓α]␈↓␈αis␈αa␈αterm.␈α If␈α␈↓αp␈↓␈αis␈αa␈αsentence␈αand␈α␈↓αt␈↓¬1␈↓α␈↓␈αand␈α
␈↓αt␈↓¬2␈↓
␈↓ ↓H␈↓are␈α∂terms,␈α∞then␈α∂␈↓βif␈↓α p ␈↓βthen␈↓α t␈↓¬1␈↓α ␈↓βelse␈↓α t␈↓¬2␈↓␈α∞is␈α∂a␈α∞term.␈α∂ We␈α∞soften␈α∂the␈α∞notation␈α∂by␈α∞allowing␈α∂infix␈α∞symbols
␈↓ ↓H␈↓where this is customary.
␈↓ ↓H␈↓␈↓βSentences␈↓:␈α∩If␈α⊃␈↓αp␈↓␈α∩is␈α⊃a␈α∩predicate␈α⊃expression␈α∩taking␈α⊃␈↓αn␈↓␈α∩arguments␈α⊃and␈α∩␈↓αt␈↓¬1␈↓α, ... ,t␈↓¬n␈↓α␈↓␈α⊃are␈α∩terms,␈α⊃then
␈↓ ↓H␈↓␈↓αp[t␈↓¬1␈↓α, ... ,t␈↓¬n␈↓α]␈↓␈αis␈αa␈αsentence.␈α Equality␈αis␈αalso␈αused␈αas␈αan␈αinfixed␈αpredicate␈αsymbol␈αto␈αform␈α
sentences,
␈↓ ↓H␈↓i.e.␈α
␈↓αt␈↓¬1␈↓α = t␈↓¬2␈↓␈α
is␈α
a␈α
sentence.␈α
If␈α
␈↓αp␈↓␈α
is␈α
a␈α
sentence,␈αthen␈α
␈↓α¬p␈↓␈α
is␈α
also␈α
a␈α
sentence.␈α
If␈α
␈↓αp␈↓␈α
and␈α
␈↓αq␈↓␈α
are␈αsentences,
␈↓ ↓H␈↓then␈α␈↓αp∧q␈↓,␈α␈↓αp∨q␈↓,␈α␈↓αp⊃q␈↓,␈αand␈α␈↓αp≡q␈↓␈αare␈αsentences.␈α If␈α␈↓αp␈↓,␈α␈↓αq␈↓␈αand␈α␈↓αr␈↓␈αare␈αsentences,␈αthen␈α␈↓βif␈↓α p ␈↓βthen␈↓α q ␈↓βelse␈↓α r␈↓␈αis␈αa
␈↓ ↓H␈↓sentence.␈α⊃ If␈α⊃␈↓αx␈↓¬1␈↓α, ..., x␈↓¬n␈↓α␈↓␈α⊃are␈α∩variables,␈α⊃and␈α⊃␈↓αp␈↓␈α⊃is␈α⊃a␈α∩term,␈α⊃then␈α⊃␈↓α∀x␈↓¬1␈↓α ... x␈↓¬n␈↓α:t␈↓␈α⊃and␈α∩␈↓α∀x␈↓¬1␈↓α ... x␈↓¬n␈↓α:t␈↓␈α⊃are
␈↓ ↓H␈↓sentences.
␈↓ ↓H␈↓␈↓βFunction␈α∂expressions␈↓:␈α∂A␈α∞function␈α∂symbol␈α∂is␈α∞a␈α∂function␈α∂expression.␈α∞ If␈α∂␈↓αx␈↓¬1␈↓α, ... ,x␈↓¬n␈↓α␈↓␈α∂are␈α∞variables
␈↓ ↓H␈↓and ␈↓αt␈↓ is a term, then ␈↓α[λx␈↓¬1␈↓α, ... ,x␈↓¬n␈↓α:t]␈↓ is a function expression.
␈↓ ↓H␈↓␈↓βPredicate␈α
expressions␈↓:␈α
A␈α
predicate␈αsymbol␈α
is␈α
a␈α
predicate␈αexpression.␈α
If␈α
␈↓αx␈↓¬1␈↓α, ... ,x␈↓¬n␈↓α␈↓␈α
are␈αvariables
␈↓ ↓H␈↓and ␈↓αp␈↓ is a sentence, then ␈↓α[λx␈↓¬1␈↓α, ... ,x␈↓¬n␈↓α:p]␈↓ is a predicate expression.
␈↓ ↓H␈↓ An␈α
occurrence␈α
of␈αa␈α
variable␈α
␈↓αx␈↓␈αis␈α
called␈α
bound␈αif␈α
it␈α
is␈αin␈α
an␈α
expression␈αof␈α
one␈α
of␈α
the␈αforms
␈↓ ↓H␈↓␈↓α[λx␈↓¬1␈↓α ... x␈↓¬n␈↓α:t]␈↓,␈α␈↓α[λx␈↓¬1␈↓α ... x␈↓¬n␈↓α:p]␈↓,␈α␈↓α[∀x␈↓¬1␈↓α ... x␈↓¬n␈↓α:p]␈↓␈αor␈α␈↓α[∃x␈↓¬1␈↓α ... x␈↓¬n␈↓α:p]␈↓␈αwhere␈α␈↓αx␈↓␈αis␈αone␈αof␈αthe␈αnumbered␈α␈↓αx␈↓'s.␈α If
␈↓ ↓H␈↓not bound an occurrence is called free.
␈↓ ↓H␈↓ The␈α∞␈↓αsemantics␈↓␈α
of␈α∞first␈α
order␈α∞logic␈α
consists␈α∞of␈α∞the␈α
rules␈α∞that␈α
enable␈α∞us␈α
to␈α∞determine␈α∞when␈α
a
␈↓ ↓H␈↓sentence␈αis␈αtrue␈α
and␈αwhen␈αit␈αis␈α
false.␈α However,␈αthe␈αtruth␈α
or␈αfalsity␈αof␈αa␈α
sentence␈αis␈αrelative␈α
to␈αthe
␈↓ ↓H␈↓interpretation␈αassigned␈αto␈αthe␈αconstants,␈αthe␈α
function␈αand␈αpredicate␈αsymbols␈αand␈αthe␈α
free␈αvariables
␈↓ ↓H␈↓of the formula. We proceed as follows:
␈↓ ↓H␈↓ We␈α
begin␈α
by␈αchoosing␈α
a␈α
domain.␈α In␈α
most␈α
cases␈αwe␈α
shall␈α
consider␈αthe␈α
domain␈α
will␈αinclude␈α
the
␈↓ ↓H␈↓S-expressions␈αand␈αany␈αS-expression␈αconstants␈αappearing␈αin␈αthe␈αformula␈αstand␈αfor␈αthemselves.␈α We
␈↓ ↓H␈↓will␈α
allow␈α∞for␈α
the␈α∞possibility␈α
that␈α∞other␈α
objects␈α
than␈α∞S-expressions␈α
exist,␈α∞and␈α
some␈α∞constants␈α
may
␈↓ ↓H␈↓designate␈α⊂them.␈α⊂ Each␈α⊂function␈α⊂or␈α⊂predicate␈α⊃symbol␈α⊂is␈α⊂assigned␈α⊂a␈α⊂function␈α⊂or␈α⊂predicate␈α⊃on␈α⊂the
␈↓ ↓H␈↓␈↓ ¬pCHAPTER V␈↓ :75
␈↓ ↓H␈↓domain.␈α↔ We␈α⊗will␈α↔normally␈α⊗assign␈α↔to␈α⊗the␈α↔basic␈α⊗LISP␈α↔function␈α⊗and␈α↔predicate␈α↔symbols␈α⊗the
␈↓ ↓H␈↓corresponding␈αbasic␈αLISP␈αfunctions␈αand␈αpredicates.␈α Each␈αvariable␈αappearing␈αfree␈αin␈αa␈αsentence␈αis
␈↓ ↓H␈↓also␈αassigned␈αan␈αelemet␈αof␈αthe␈αdomain.␈α All␈αthese␈αassignments␈αconstitute␈αan␈αinterpretation,␈αand␈αthe
␈↓ ↓H␈↓truth of a sentence is relative to the interpretation.
␈↓ ↓H␈↓ The␈α⊃truth␈α⊃of␈α⊂a␈α⊃sentence␈α⊃is␈α⊃determined␈α⊂from␈α⊃the␈α⊃values␈α⊂of␈α⊃its␈α⊃constituents␈α⊃by␈α⊂evaluating
␈↓ ↓H␈↓successively␈α∂larger␈α∂subexpressions.␈α⊂ The␈α∂rules␈α∂for␈α⊂handling␈α∂functions␈α∂and␈α⊂predicates,␈α∂conditional
␈↓ ↓H␈↓expressions,␈α
equality,␈α
and␈α
Boolean␈α
expressions␈αare␈α
exactly␈α
the␈α
same␈α
as␈αthose␈α
we␈α
have␈α
used␈α
in␈αthe
␈↓ ↓H␈↓previous chapters. We need only explain quantifiers:
␈↓ ↓H␈↓ ␈↓α∀x␈↓¬1␈↓α ... x␈↓¬n␈↓α:e␈↓␈α
is␈α
assigned␈αtrue␈α
if␈α
and␈αonly␈α
if␈α
␈↓αe␈↓␈α
is␈αassigned␈α
true␈α
for␈αall␈α
assignments␈α
of␈αelements␈α
of
␈↓ ↓H␈↓the␈α∞domain␈α∞to␈α∞the␈α∞␈↓αx␈↓'s.␈α∞ If␈α∞␈↓αe␈↓␈α∞has␈α∞free␈α∞variables␈α∞that␈α∞are␈α∞not␈α∞among␈α∞the␈α∞␈↓αx␈↓'s,␈α∞then␈α∞the␈α∞truth␈α∞of␈α
the
␈↓ ↓H␈↓sentence␈αdepends␈αon␈αthe␈αvalues␈αassigned␈αto␈αthese␈αremaining␈αfree␈αvariables.␈α ␈↓α∃x␈↓¬1␈↓α ... x␈↓¬n␈↓α:e␈↓␈αis␈αassigned
␈↓ ↓H␈↓true␈αif␈αand␈αonly␈αif␈α␈↓αe␈↓␈αis␈αassigned␈αtrue␈αfor␈α␈↓αsome␈↓␈αassignment␈αof␈αvalues␈αin␈αthe␈αdomain␈αto␈αthe␈α␈↓αx␈↓'s.␈α Free
␈↓ ↓H␈↓variables are handled just as before.
␈↓ ↓H␈↓ ␈↓αλx␈↓¬1␈↓α ... x␈↓¬n␈↓α:u␈↓␈α∩is␈α∩assigned␈α⊃a␈α∩function␈α∩or␈α∩predicate␈α⊃according␈α∩to␈α∩whether␈α∩␈↓αu␈↓␈α⊃is␈α∩a␈α∩term␈α∩or␈α⊃a
␈↓ ↓H␈↓sentence.␈α∞ The␈α∞value␈α∂of␈α∞␈↓α[λx␈↓¬1␈↓α ... x␈↓¬n␈↓α:u][t␈↓¬1␈↓α,...,t␈↓¬n␈↓α]␈α∞is␈α∂obtained␈α∞by␈α∞evaluating␈α∂the␈α∞t␈↓'s␈α∞and␈α∂using␈α∞these
␈↓ ↓H␈↓values␈αas␈αvalues␈α
of␈αthe␈α␈↓αx␈↓'s␈αin␈α
the␈αevaluation␈αof␈α
␈↓αu␈↓.␈α If␈α␈↓αu␈↓␈αhas␈α
free␈αvariables␈αin␈α
addition␈αto␈αthe␈α␈↓αx␈↓'s,␈α
the
␈↓ ↓H␈↓function assigned will depend on them too.
␈↓ ↓H␈↓ Those␈αwho␈αare␈αfamiliar␈αwith␈αthe␈αlambda␈αcalculus␈αshould␈αnote␈αthat␈αλ␈αis␈αbeing␈αused␈αhere␈αin␈αa
␈↓ ↓H␈↓very␈αlimited␈αway.␈α Namely,␈αthe␈αvariables␈αin␈αa␈αlambda-expression␈αtake␈αonly␈αelements␈αof␈αthe␈αdomain
␈↓ ↓H␈↓as␈α
values,␈α
whereas␈αthe␈α
essence␈α
of␈α
the␈αlambda␈α
calculus␈α
is␈α
that␈αthey␈α
take␈α
arbitrary␈α
functions␈αas␈α
values.
␈↓ ↓H␈↓We may call these restricted lambda expressions ␈↓αfirst order lambdas␈↓.
␈↓ ↓H␈↓2. ␈↓βConditional forms.␈↓
␈↓ ↓H␈↓ All the properties we shall use of conditional forms follow from the relation
␈↓ ↓H␈↓ ␈↓α[p ⊃ [␈↓βif␈↓α p ␈↓βthen ␈↓αa ␈↓βelse␈↓α b] = a] ∧ [¬p ⊃ [␈↓βif␈↓α p ␈↓βthen␈↓α a ␈↓βelse␈↓αb] = b]␈↓.
␈↓ ↓H␈↓(If␈αwe␈αweren't␈αadhering␈αto␈αthe␈αrequirement␈αthat␈αall␈αterms␈αbe␈αdefined␈αfor␈αall␈αvalues␈αof␈αthe␈αvariables,
␈↓ ↓H␈↓the situation would be more complicated).
␈↓ ↓H␈↓ It is, however, worthwhile to list separately some properties of conditional forms.
␈↓ ↓H␈↓ First we have the obvious
␈↓ ↓H␈↓ ␈↓βif␈↓∧ T ␈↓βthen␈↓α a ␈↓βelse␈↓α b = a␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓βif␈↓∧ F ␈↓βthen␈↓α a ␈↓βelse␈↓α b = b␈↓.
␈↓ ↓H␈↓␈↓ ¬pCHAPTER V␈↓ :76
␈↓ ↓H␈↓ Next we have a ␈↓αdistributive law␈↓ for functions applied to conditional forms, namely
␈↓ ↓H␈↓ ␈↓αf[␈↓βif␈↓α p ␈↓βthen␈↓α a ␈↓βelse␈↓α b] = ␈↓βif␈↓α p ␈↓βthen␈↓α f[a] ␈↓βelse␈↓α f[b]␈↓.
␈↓ ↓H␈↓This␈αapplies␈αto␈αpredicates␈αas␈αwell␈αas␈αfunctions␈αand␈αcan␈αalso␈αbe␈αused␈αwhen␈αone␈αof␈αthe␈αarguments␈αof
␈↓ ↓H␈↓a␈αfunction␈αof␈α
several␈αarguments␈αis␈αa␈α
conditional␈αform.␈α It␈α
also␈αapplies␈αwhen␈αone␈α
of␈αthe␈αterms␈α
of␈αa
␈↓ ↓H␈↓conditional form is itself a conditional form.
␈↓ ↓H␈↓Thus
␈↓ ↓H␈↓ ␈↓βif␈↓α [␈↓βif␈↓α p ␈↓βthen␈↓α q ␈↓βelse␈↓α r] ␈↓βthen␈↓α a ␈↓βelse␈↓α b = ␈↓βif␈↓α p ␈↓βthen␈↓α [␈↓βif␈↓α q ␈↓βthen␈↓α a ␈↓βelse␈↓α b] ␈↓βelse␈↓α [␈↓βif␈↓α r ␈↓βthen␈↓α a ␈↓βelse␈↓α b]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓βif␈↓α p ␈↓βthen␈↓α [␈↓βif␈↓α q ␈↓βthen␈↓α a ␈↓βelse␈↓α b] ␈↓βelse␈↓α c = ␈↓βif␈↓α q ␈↓βthen␈↓α [␈↓βif␈↓α p ␈↓βthen␈↓α a ␈↓βelse␈↓α c] ␈↓βelse␈↓α [␈↓βif␈↓α p ␈↓βthen␈↓α b ␈↓βelse␈↓α c]␈↓.
␈↓ ↓H␈↓ When␈α
the␈α
expressions␈α
following␈α
␈↓βthen␈↓␈α
and␈α
␈↓βelse␈↓␈α
are␈α
sentences,␈α
then␈α
the␈α
conditional␈α
form␈α
can
␈↓ ↓H␈↓be replaced by a sentence according to
␈↓ ↓H␈↓ ␈↓α[␈↓βif␈↓α p ␈↓βthen␈↓α q ␈↓βelse␈↓α r] ≡ [p ∧ q] ∨ [¬p ∧ r]␈↓.
␈↓ ↓H␈↓These␈αtwo␈αrules␈αpermit␈αeliminating␈αconditional␈αforms␈αfrom␈αsentences␈αby␈αfirst␈αusing␈αdistributivity␈αto
␈↓ ↓H␈↓move␈αthe␈αconditionals␈αto␈αthe␈α
outside␈αof␈αany␈αfunctions␈αand␈α
then␈αreplacing␈αthe␈αconditional␈αform␈αby␈α
a
␈↓ ↓H␈↓Boolean expression.
␈↓ ↓H␈↓ Note␈αthat␈αthe␈αelimination␈αof␈αconditional␈αforms␈αmay␈αincrease␈αthe␈αsize␈αof␈αa␈αsentence,␈αbecause␈α␈↓αp␈↓
␈↓ ↓H␈↓occurs␈α
twice␈α
in␈α
the␈α
right␈αhand␈α
side␈α
of␈α
the␈α
above␈α
equivalence.␈α In␈α
the␈α
most␈α
unfavorable␈α
case,␈α
␈↓αp␈↓␈αis
␈↓ ↓H␈↓dominates␈α∪the␈α∩size␈α∪of␈α∩the␈α∪expression␈α∩so␈α∪that␈α∩writing␈α∪it␈α∩twice␈α∪almost␈α∩doubles␈α∪the␈α∩size␈α∪of␈α∩the
␈↓ ↓H␈↓expression.
␈↓ ↓H␈↓ Suppose␈α
that␈α
␈↓αa␈↓␈αand␈α
␈↓αb␈↓␈α
in␈α
␈↓βif␈↓α p ␈↓βthen␈↓α a ␈↓βelse␈↓α b␈↓␈αare␈α
expressions␈α
that␈αmay␈α
contain␈α
the␈α
sentence␈α␈↓αp␈↓.
␈↓ ↓H␈↓Occurrences␈αof␈α␈↓αp␈↓␈αin␈α␈↓αa␈↓␈αcan␈αbe␈αreplaced␈αby␈α␈↓∧T␈↓,␈αand␈αoccurrences␈αof␈α␈↓αp␈↓␈αin␈α␈↓αb␈↓␈αcan␈αbe␈αreplaced␈αby␈α␈↓∧F␈↓.␈α This
␈↓ ↓H␈↓follows from the fact that ␈↓αa␈↓ is only evaluated if ␈↓αp␈↓ is true and ␈↓αb␈↓ is evaluated only if ␈↓αp␈↓ is false.
␈↓ ↓H␈↓ This␈α⊃leads␈α⊂to␈α⊃a␈α⊂strengthened␈α⊃form␈α⊂of␈α⊃the␈α⊂law␈α⊃of␈α⊂replacement␈α⊃of␈α⊂equals␈α⊃by␈α⊃equals.␈α⊂ The
␈↓ ↓H␈↓ordinary␈αform␈αof␈αthe␈αlaw␈αsays␈α
that␈αif␈αwe␈αhave␈α␈↓αe = e'␈↓,␈αthen␈αwe␈α
can␈αreplace␈αany␈αoccurrence␈αof␈α␈↓αe␈↓␈αin␈α
an
␈↓ ↓H␈↓expression␈α∩by␈α∩an␈α∩occurrence␈α∩of␈α⊃␈↓αe'␈↓.␈α∩ However,␈α∩if␈α∩we␈α∩want␈α∩to␈α⊃replace␈α∩␈↓αe␈↓␈α∩by␈α∩␈↓αe'␈↓␈α∩within␈α∩␈↓αa␈↓␈α⊃within
␈↓ ↓H␈↓␈↓βif␈↓α p ␈↓βthen␈↓α a ␈↓βelse␈↓α b␈↓,␈α∂then␈α∞we␈α∂need␈α∞only␈α∂prove␈α∞␈↓αp ⊃ e =e'␈↓,␈α∂and␈α∞to␈α∂make␈α∞the␈α∂replacement␈α∞within␈α∂␈↓αb␈↓␈α∞we
␈↓ ↓H␈↓need only prove ␈↓α¬p ⊃ e = e'␈↓.
␈↓ ↓H␈↓ Additional␈α∃facts␈α∃about␈α∃conditional␈α∃forms␈α∃are␈α∃given␈α∃in␈α∃(McCarthy␈α∃1963a)␈α⊗including␈α∃a
␈↓ ↓H␈↓discussion␈αof␈α
canonical␈αforms␈α
that␈αparallels␈αthe␈α
canonical␈αforms␈α
of␈αBoolean␈α
forms.␈α Any␈αquestion␈α
of
␈↓ ↓H␈↓equivalence␈α⊂of␈α⊂conditional␈α∂forms␈α⊂is␈α⊂decidable␈α∂by␈α⊂truth␈α⊂tables␈α∂analogously␈α⊂to␈α⊂the␈α⊂decidability␈α∂of
␈↓ ↓H␈↓Boolean forms.
␈↓ ↓H␈↓␈↓ ¬pCHAPTER V␈↓ :77
␈↓ ↓H␈↓3. ␈↓βLambda-expressions.␈↓
␈↓ ↓H␈↓ The␈α∂only␈α∂additional␈α∂rule␈α∂required␈α∂for␈α∂handling␈α∂lambda-expressions␈α∂in␈α∂first␈α∂order␈α⊂logic␈α∂is
␈↓ ↓H␈↓called ␈↓αlambda-conversion␈↓, essentially
␈↓ ↓H␈↓ ␈↓α[λx:e][a] =␈↓ <the result of substituting ␈↓αe␈↓ for ␈↓αx␈↓ in ␈↓αa␈↓>.
␈↓ ↓H␈↓As examples of this rule, we have
␈↓ ↓H␈↓ ␈↓α[λx:␈↓βa␈↓α x . y][u . v] = [␈↓βa␈↓α[u . v]] . y␈↓.
␈↓ ↓H␈↓However,␈α∞a␈α∞complication␈α
requires␈α∞modifying␈α∞the␈α
rule.␈α∞ Namely,␈α∞we␈α
can't␈α∞substitute␈α∞for␈α∞a␈α
variable
␈↓ ↓H␈↓and␈αexpression␈αthat␈αhas␈αa␈αfree␈αvariable␈αinto␈αa␈αcontext␈αin␈αwhich␈αthat␈αfree␈αvariable␈αis␈αbound.␈α Thus
␈↓ ↓H␈↓it␈α⊂would␈α⊂be␈α⊂wrong␈α⊂to␈α⊂substitute␈α⊂␈↓αx + y␈↓␈α⊃for␈α⊂␈↓αx␈↓␈α⊂in␈α⊂␈↓α∀y:[x + y = z]␈↓␈α⊂or␈α⊂into␈α⊂the␈α⊃term␈α⊂␈↓α[λy:x + y][u + v]␈↓.
␈↓ ↓H␈↓Before␈α
doing␈α
the␈αsubstitution,␈α
the␈α
variable␈α␈↓αy␈↓␈α
would␈α
have␈αto␈α
be␈α
replaced␈αin␈α
all␈α
its␈αbound␈α
occurrences
␈↓ ↓H␈↓by a fresh variable.
␈↓ ↓H␈↓ Lambda-expressions␈α⊗can␈α⊗always␈α⊗be␈α⊗eliminated␈α⊗from␈α⊗sentences␈α⊗and␈α⊗terms␈α⊗by␈α∃lambda-
␈↓ ↓H␈↓conversion,␈αbut␈αthe␈αexpression␈αmay␈αincrease␈αgreatly␈αin␈αlength␈αif␈αa␈αlengthy␈αterm␈αreplaces␈αa␈αvariable
␈↓ ↓H␈↓that␈α∂occurs␈α∂more␈α∞than␈α∂once␈α∂in␈α∞␈↓αe␈↓.␈α∂ It␈α∂is␈α∞easy␈α∂to␈α∂make␈α∞an␈α∂expression␈α∂of␈α∞length␈α∂␈↓αn␈↓␈α∂whose␈α∂length␈α∞is
␈↓ ↓H␈↓increased to 2␈↓εn␈↓ by converting its ␈↓αn␈↓ nested lambda-expressions.
␈↓ ↓H␈↓4. ␈↓βAlgebraic axioms for S-expressions and lists.␈↓
␈↓ ↓H␈↓ The␈α⊂algebraic␈α⊂facts␈α⊂about␈α⊂S-expressions␈α⊂are␈α∂expressed␈α⊂by␈α⊂the␈α⊂following␈α⊂sentences␈α⊂of␈α∂first
␈↓ ↓H␈↓order logic:
␈↓ ↓H␈↓ ␈↓α∀x.(issexp x ⊃ ␈↓βat␈↓α x ∨ (issexp ␈↓βa␈↓α x ∧ issexp ␈↓βd␈↓α x ∧ x = (␈↓βa␈↓α x . ␈↓βd␈↓α x)))␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓α∀x y.(issexp x ∧ issexp y ⊃ issexp(x.y) ∧ ¬␈↓βat␈↓α(x.y) ∧ x = ␈↓βa␈↓α(x.y) ∧ y = ␈↓βd␈↓α(x.y))␈↓.
␈↓ ↓H␈↓Here␈α
␈↓αissexp␈α
e␈↓␈αasserts␈α
that␈α
the␈α
object␈α␈↓αe␈↓␈α
is␈α
an␈αS-expression␈α
so␈α
that␈α
the␈αsentences␈α
used␈α
in␈α
proving␈αa
␈↓ ↓H␈↓particular␈α∞program␈α
correct␈α∞can␈α
involve␈α∞other␈α
kinds␈α∞of␈α∞entities␈α
as␈α∞well.␈α
If␈α∞we␈α
can␈α∞assume␈α∞that␈α
all
␈↓ ↓H␈↓objects␈α
are␈α
S-expressions␈α
or␈α∞can␈α
declare␈α
certain␈α
variables␈α∞as␈α
ranging␈α
only␈α
over␈α∞S-expressions,␈α
we
␈↓ ↓H␈↓can simplify the axioms to
␈↓ ↓H␈↓ ␈↓α∀x.[␈↓βat␈↓α x ∨ x = [␈↓βa␈↓α x . ␈↓βd␈↓α x]]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓α∀x y.[¬␈↓βat␈↓α[x.y] ∧ x = ␈↓βa␈↓α[x.y] ∧ y = ␈↓βd␈↓α[x.y]]␈↓.
␈↓ ↓H␈↓␈↓ ¬pCHAPTER V␈↓ :78
␈↓ ↓H␈↓ The algebraic facts about lists are expressed by the following sentences of first order logic:
␈↓ ↓H␈↓ ␈↓α∀x. islist x ⊃ x = ␈↓∧NIL␈↓α ∨ islist ␈↓βd ␈↓αx␈↓,
␈↓ ↓H␈↓ ␈↓α∀x y. islist y ⊃ islist[x . y]␈↓,
␈↓ ↓H␈↓ ␈↓α∀x y. islist y ⊃ ␈↓βa␈↓α[x . y] = x ∧ ␈↓βd␈↓α[x.y] = y␈↓.
␈↓ ↓H␈↓We␈α
can␈α
rarely␈α
assume␈αthat␈α
everything␈α
is␈α
a␈αlist,␈α
because␈α
the␈α
lists␈αusually␈α
contain␈α
atoms␈α
which␈αare␈α
not
␈↓ ↓H␈↓themselves lists.
␈↓ ↓H␈↓ These␈α∞axioms␈α∞are␈α∞analogous␈α∞to␈α∞the␈α∞algebraic␈α∞part␈α∞of␈α∞Peano's␈α∞axioms␈α∞for␈α∂the␈α∞non-negative
␈↓ ↓H␈↓integers.␈α The␈α
analogy␈αcan␈α
be␈αmade␈α
clear␈αif␈α
we␈αwrite␈α
Peano's␈αaxioms␈α
using␈α␈↓αn'␈↓␈α
for␈αthe␈α
successor␈αof␈α
␈↓αn␈↓
␈↓ ↓H␈↓and ␈↓αn␈↓ε-␈↓ for the predecessor of ␈↓αn␈↓. Peano's algebraic axioms then become
␈↓ ↓H␈↓ ␈↓α∀n: n' ≠ 0␈↓,
␈↓ ↓H␈↓ ␈↓α∀n: (n')␈↓ε-␈↓α = n␈↓,
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓α∀n: n ≠ 0 ⊃ (n␈↓ε-␈↓α)' = n␈↓.
␈↓ ↓H␈↓Integers␈αspecialize␈α lists␈α
if␈αwe␈αidentify␈α0␈α
with␈α␈↓∧NIL␈↓␈αand␈α
assume␈αthat␈αthere␈αis␈α
only␈αone␈αobject␈α
(say␈α1)
␈↓ ↓H␈↓that can serve as a list element. Then ␈↓αn' = cons[1,n]␈↓, and ␈↓αn␈↓ε-␈↓α = ␈↓βd␈↓α n␈↓.
␈↓ ↓H␈↓ Clearly␈α∂S-expressions␈α∂and␈α∂lists␈α∂satisfy␈α∂the␈α∂axioms␈α∂given␈α∂for␈α∂them,␈α∂but␈α∂unfortunately␈α∞these
␈↓ ↓H␈↓algebraic␈α∂axioms␈α⊂are␈α∂insufficient␈α∂to␈α⊂characterize␈α∂them.␈α⊂ For␈α∂example,␈α∂consider␈α⊂a␈α∂domain␈α⊂of␈α∂one
␈↓ ↓H␈↓element ␈↓↓a␈↓ satisfying
␈↓ ↓H␈↓ ␈↓βa␈↓α a = ␈↓βd␈↓α a = a . a = a␈↓.
␈↓ ↓H␈↓It␈α
satisfies␈α
the␈αalgebraic␈α
axioms␈α
for␈αS-expressions.␈α
We␈α
can␈α
exclude␈αit␈α
by␈α
an␈αaxiom␈α
␈↓α∀x.(␈↓βa␈↓α␈α
x␈α
≠␈αx)␈↓,
␈↓ ↓H␈↓but␈α
this␈α∞won't␈α
exclude␈α
other␈α∞circular␈α
list␈α
structures␈α∞that␈α
eventually␈α
return␈α∞to␈α
the␈α
same␈α∞element␈α
by
␈↓ ↓H␈↓some␈α
␈↓βa-d␈↓␈αchain.␈α
Actually␈α
we␈αwant␈α
to␈α
exclude␈αall␈α
infinite␈α
chains,␈αbecause␈α
most␈α
LISP␈αprograms␈α
won't
␈↓ ↓H␈↓terminate␈α∞unless␈α
every␈α∞␈↓βa-d␈↓␈α
chain␈α∞eventually␈α
terminates␈α∞in␈α
an␈α∞atom.␈α
This␈α∞cannot␈α
be␈α∞done␈α∞by␈α
any
␈↓ ↓H␈↓finite set of axioms.
␈↓ ↓H␈↓5. ␈↓βAxiom schemas of induction.␈↓
␈↓ ↓H␈↓ ␈α∞ In␈α
order␈α∞to␈α
exclude␈α∞infinite␈α
list␈α∞structures␈α
we␈α∞need␈α
axioms␈α∞of␈α
induction␈α∞analogous␈α
to
␈↓ ↓H␈↓Peano's induction axiom. Peano's axiom is ordinarily written
␈↓ ↓H␈↓ ␈↓αP(0) ∧ ∀n:(P(n) ⊃ P(n')) ⊃ ∀n:P(n)␈↓.
␈↓ ↓H␈↓␈↓ ¬pCHAPTER V␈↓ :79
␈↓ ↓H␈↓Here␈α∞␈↓αP(n)␈↓␈α∞is␈α∞an␈α∞arbitrary␈α∞predicate␈α∞of␈α∞integers,␈α∞and␈α∞we␈α∞get␈α∞particular␈α∞instances␈α∞of␈α∞the␈α∞axiom␈α∞by
␈↓ ↓H␈↓substituting particular predicates.
␈↓ ↓H␈↓Peano's induction schema can also be written
␈↓ ↓H␈↓ ␈↓α∀n:(n = 0 ∨ P(n␈↓ε-␈↓α) ⊃ P(n)) ⊃ ∀n:P(n)␈↓,
␈↓ ↓H␈↓and the equivalence of the two forms is easily proved.
␈↓ ↓H␈↓ The S-expression analog is
␈↓ ↓H␈↓ ␈↓α∀x:[issexp x ⊃ [␈↓βat␈↓α x ∨ P[␈↓βa␈↓α x] ∧ P[␈↓βd␈↓α x] ⊃ P[x]]] ⊃ ∀x:[issexp x ⊃ P[x]]␈↓,
␈↓ ↓H␈↓or, assuming everything is an S-expression
␈↓ ↓H␈↓ ␈↓α∀x:[␈↓βat␈↓α x ∨ P[␈↓βa␈↓α x] ∧ P[␈↓βd␈↓α x] ⊃ P[x]] ⊃ ∀x:P[x]␈↓.
␈↓ ↓H␈↓ The corresponding axiom schema for lists is
␈↓ ↓H␈↓ ␈↓α∀u:[islist u ⊃ [␈↓βn␈↓α u ∨ P[␈↓βd␈↓α u] ⊃ P[u]]] ⊃ ∀u:[islist u ⊃ P[u]]␈↓.
␈↓ ↓H␈↓ These␈α⊂schemas␈α⊂are␈α⊂called␈α∂principles␈α⊂of␈α⊂␈↓αstructural␈α⊂induction␈↓,␈α∂since␈α⊂the␈α⊂induction␈α⊂is␈α⊂on␈α∂the
␈↓ ↓H␈↓structure of the entities involved.
␈↓ ↓H␈↓6. ␈↓βProofs by structural induction.␈↓
␈↓ ↓H␈↓ Recall that the operation of appending two lists is defined by
␈↓ ↓H␈↓!!si2: ␈↓αu * v ← ␈↓βif n␈↓α u ␈↓βthen␈↓α v ␈↓βelse a␈↓α u . [␈↓βd␈↓α u * v]␈↓.
␈↓ ↓H␈↓Because␈α
␈↓αu*v␈↓␈α
is␈α
defined␈α
for␈α∞all␈α
␈↓αu␈↓␈α
and␈α
␈↓αv␈↓,␈α
i.e.␈α
the␈α∞computation␈α
described␈α
above␈α
terminates␈α
for␈α∞all␈α
␈↓αu␈↓
␈↓ ↓H␈↓and ␈↓αv␈↓, we can replace ({[5] si2}) by the sentence
␈↓ ↓H␈↓!!si1: ␈↓α∀u v:[islist u ∧ islist v ⊃ [u * v = ␈↓βif n␈↓α u ␈↓βthen␈↓α v ␈↓βelse a␈↓α u . [␈↓βd␈↓α u * v]]]␈↓.
␈↓ ↓H␈↓Now␈α
suppose␈αwe␈α
would␈αlike␈α
to␈αprove␈α
␈↓α∀v:[␈↓∧NIL␈↓α␈α*␈α
v␈α=␈α
v]␈↓.␈α This␈α
is␈αquite␈α
trivial;␈αwe␈α
need␈αonly␈α
substitute
␈↓ ↓H␈↓␈↓∧NIL␈↓ for ␈↓αx␈↓ in ({[5] si1}), getting
␈↓ ↓H␈↓→␈↓∧NIL␈↓α * v ∂(15) = ␈↓βif n ␈↓∧NIL ␈↓βthen␈↓α v ␈↓βelse a ␈↓∧NIL␈↓α . [␈↓βd ␈↓∧NIL␈↓α * v]
␈↓ ↓H␈↓α∂(15) = v␈↓.
␈↓ ↓H␈↓Next␈αconsider␈αproving␈α␈↓α∀u:[u␈α*␈α␈↓∧NIL␈↓α␈α
=␈αu]␈↓.␈α This␈αcannot␈αbe␈αdone␈α
by␈αsimple␈αsubstitution,␈αbut␈αit␈αcan␈α
be
␈↓ ↓H␈↓done as follows: First substitute ␈↓αλu:[u * ␈↓∧NIL␈↓α = u]␈↓ for ␈↓αP␈↓ in the induction schema
␈↓ ↓H␈↓␈↓ ¬pCHAPTER V␈↓ :80
␈↓ ↓H␈↓ ␈↓α∀u:[islist u ⊃ [␈↓βn␈↓α u ∨ P[␈↓βd␈↓α u] ⊃ P[u]]] ⊃ ∀u:[islist u ⊃ P[u]]␈↓,
␈↓ ↓H␈↓getting
␈↓ ↓H␈↓ ␈↓α∀u:[islist␈α
u␈α⊃␈α
[␈↓βn␈↓α␈α
u␈α∨␈α
λu:[u␈α*␈α
␈↓∧NIL␈↓α␈α
=␈αu][␈↓βd␈↓α␈α
u]␈α
⊃␈αλu:[u␈α
*␈α␈↓∧NIL␈↓α␈α
=␈α
u][u]]]␈α⊃␈α
∀u:[islist␈α
u␈α⊃␈α
λu:[u␈α*␈α
␈↓∧NIL␈↓α
␈↓ ↓H␈↓α= u][u]]␈↓.
␈↓ ↓H␈↓Carrying out the indicated lambda conversions makes this
␈↓ ↓H␈↓!!si3: ␈↓α∀u:[islist u ⊃ [qn u ∨ qd u * qnil = qd u] ⊃ u * qnil = u] ⊃ ∀u:[islist u ⊃ u * qnil = u]␈↓.
␈↓ ↓H␈↓ Next␈αwe␈αmust␈αuse␈αthe␈αrecursive␈αdefinition␈αof␈α␈↓αu*v␈↓.␈α There␈αare␈αtwo␈αcases␈αaccording␈αto␈αwhether
␈↓ ↓H␈↓qn␈α␈↓αu␈↓␈αor␈αnot.␈α In␈αthe␈αfirst␈αcase,␈αwe␈αsubstitute␈αqnil␈αfor␈α␈↓αv␈↓␈αand␈αget␈αqnil*qnil_=_qnil,␈αand␈αin␈αthe␈αsecond
␈↓ ↓H␈↓case␈α
we␈αuse␈α
the␈αhypothesis␈α
qd_u_*_qnil_=_qd_u␈αand␈α
the␈αthird␈α
algebraic␈αaxiom␈α
for␈αlists␈α
to␈αmake␈α
the
␈↓ ↓H␈↓simplification
␈↓ ↓H␈↓!!si4: ␈↓αqa u . [qd u * qnil] = qa u . qd u = u.␈↓
␈↓ ↓H␈↓Combining␈α⊃the␈α⊃cases␈α⊂gives␈α⊃the␈α⊃hypothesis␈α⊃of␈α⊂({eq␈α⊃si3})␈α⊃and␈α⊃hence␈α⊂its␈α⊃conclusion,␈α⊃which␈α⊃is␈α⊂the
␈↓ ↓H␈↓statement to be proved.